noritsuguの日記

2009-04-21``Algorithms'' (46) 3.1.3 Lazy data structures

add x y  = x + y
double x = add x x


data List a = Cons a (List a) | Nil

my_map f Nil         = Nil
my_map f (Cons x xs) = Cons (f x) (my_map f xs)

my_head (Cons x xs)  = x

で、

*Main> my_head (my_map double (Cons 1 (Cons 2 (Cons 3 Nil))))
2

add x y  = x + y
double x = add x x


data List a = Cons !a (List a) | Nil

my_map f Nil         = Nil
my_map f (Cons x xs) = Cons (f x) (my_map f xs)

my_head (Cons x xs)  = x

で、

*Main> my_head (my_map double (Cons 1 (Cons 2 (Cons 3 Nil))))
2

add x y  = x + y
double x = add x x


data List a = Cons a !(List a) | Nil

my_map f Nil         = Nil
my_map f (Cons x xs) = Cons (f x) (my_map f xs)

my_head (Cons x xs)  = x

で、

*Main> my_head (my_map double (Cons 1 (Cons 2 (Cons 3 Nil))))
2
トラックバック - http://haskell.g.hatena.ne.jp/noritsugu/20090421