Skip to content

Instantly share code, notes, and snippets.

@einblicker
Created January 18, 2012 09:36
Show Gist options
  • Save einblicker/1632170 to your computer and use it in GitHub Desktop.
Save einblicker/1632170 to your computer and use it in GitHub Desktop.
多相再帰の例
data Seq a = Nil | Cons a (Seq (a,a))
-- 多相再帰の型推論は完全ではないので、
-- このように型アノテーションをコメントアウトすると
-- コンパイルできない
-- len :: Seq a -> Int
len Nil = 0
len (Cons x s) = 1 + 2 * (len s)
pair f (x,y) = (f x, f y)
-- mapseq :: (a -> b) -> Seq a -> Seq b
mapseq f Nil = Nil
mapseq f (Cons x xs) = Cons (f x) (mapseq (pair f) xs)
main = return ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment