-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathProgram.fs
More file actions
58 lines (41 loc) · 1.49 KB
/
Program.fs
File metadata and controls
58 lines (41 loc) · 1.49 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
type ListOf<'T> = | Cons of head:'T * ListOf<'T> | Empty
let x = Empty // An empty list
let y = Cons(1, Empty) // Single item in list
let z = Cons(1, Cons(2, Empty)) // Two item in list
printfn "x=%O" x
printfn "y=%O" y
printfn "z=%O" z
let nil() = Empty
let createTwoItemList a b = Cons(a, Cons(b, Empty))
let a = createTwoItemList 1 2
printfn "a=%O" a
let isNil<'T>(list: ListOf<'T>): bool=
match list with
| Empty -> true
| _ -> false
let head<'T>(list: ListOf<'T>): Option<'T> =
match list with
| Cons( head:'T , _) -> Some(head)
| _ -> None
let tail<'T>(list: ListOf<'T>): ListOf<'T> =
match list with
| Cons( head:'T , tail) -> tail
| Empty -> Empty
let sum(list: ListOf<int>): int =
let rec sumList(list: ListOf<int>, total: int): int =
match head(list) with
| Some(x) -> sumList(tail(list), total + x)
| None -> total
sumList(list, 0)
let rec fold<'L, 'A>(list: ListOf<'L>)(acc: 'A)(folder: ('A*'L)->'A ): 'A =
match head(list) with
| Some(x) -> fold(tail(list))(folder(acc , x))(folder)
| None -> acc
printfn "isNil for empty list is %O" (isNil(x))
printfn "isNil for non empty list is %O" (isNil(y))
printfn "head for a list with stuff in %O" (head(y))
printfn "tail for a list with stuff in %O" (tail(z))
printfn "tail for a list which is empty %O" (tail(x))
printfn "Sum a list = %O" (sum(z))
printfn "Fold a list = %O" (fold(z)(0)(fun (a,b) -> a + b))
printfn "Fold a list into a string = %O" (fold(z)("")(fun (a,b) -> sprintf "%s%d" a b))