-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.mana
41 lines (31 loc) · 1.17 KB
/
list.mana
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
------------------------------------------------------------------------------
--
-- List
--
------------------------------------------------------------------------------
namespace List
import Primitive
------------------------------------------------------------------------------
-- List
------------------------------------------------------------------------------
type List a = Nil | Cons a (List a)
head : List a -> Maybe a
head (Nil ) = None
head (Cons x xs) = Some x
map : (a -> b) -> List a -> List b
map f (Nil ) = Nil
map f (Cons x xs) = Cons (f x) (map f xs)
zipWith : (a -> b -> c) -> List a -> List b -> List c
zipWith f (Cons x xs) (Cons y ys) = Cons (f x y) (zipWith f xs ys)
zipWith f _ _ = Nil
------------------------------------------------------------------------------
-- main
------------------------------------------------------------------------------
xs : List Int
xs = Cons 3 (Cons 5 Nil)
main = do
print <| xs
print <| head (Nil : List Bool)
print <| head xs
print <| map square xs
print <| zipWith (+) xs (map square xs)