tree = (l, r) ->
node = (l, r) ->
fmap: (f, g) ->
f l.fmap(f, g), r.fmap(f, g)
leaf = (a) ->
fmap: (f, g) -> g a
if l?.fmap and r?.fmap
return node l, r
return leaf l
showTree = (t) ->
f = (l, r) -> "(#{l}, #{r})"
g = (a) -> "#{a}"
return t.fmap f, g
a = tree 1
b = tree 2
c = tree a, b
d = tree 3
e = tree c, d
console.log showTree e
# 出力
((1, 2), 3)