-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtestProgram.lp
160 lines (116 loc) · 3 KB
/
testProgram.lp
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
datatype Gender := Man a b | Woman Cake | Frog
datatype D a := C (Cake a)
-- overloading a f functions
function id : Int
id := 5
function minz : IO
minz (C x) := case x of
(Sweet y) -> print "sweet y"
Lie -> print "LIE"
_ -> print "NADA"
minz _ := print "gick inge bra"
function testGen : Gender -> (IO Char)
testGen Frog := print "hello"
-- testGen x := case x of
-- (Woman a) -> print "She's a lady"
-- (Man a) -> print "Now, that's a man"
-- (Frog a) -> print "A big frog"
-- _ -> print "Something else"
function testFoldl : Int
testFoldl := foldl (\acc x -> acc + x) 0
function addL : [Int] -> [Int] -> [Int]
great x := x:[2,3,4]
function great : Boolean
great := 7 < 8 || 3 > 6
function lett : Int
lett := let x := pw
in x + 2
function nt : Boolean
nt := not True
function dv : Int
dv := 5 + 2
function pw : Int
pw := 5 ^ (-2)
function putStrLn : (IO Char)
putStrLn := (print "hej" >> print " da, ") >> print " mamma"
function testLam : [Int] -> Int
testLam := (\y -> 2+(head y)) (tail [1,2,3])
function testLamMap : [Int]
testLam := map (\x -> x+2) [1,2,3]
function sumFst : [(Int,Int)] -> Int
sumFst (x:[]) := fst x
sumFst (x:xs) := (fst x) + (sumFst xs)
function sumFstList : [[Int]] -> Int
sumFstList (x:[]) := head x
sumFstList (x:xs) := (head x) + (sumFstList xs)
function fst : (Int,Int) -> Int
fst (x,y) := x
function sum : [Int] -> Int
sum (x:[]) := x
sum (x:xs) := x + sum xs
function head : [Int] -> Int
head (x:[]) := x
head (x:xs) := x
function trd : [Int] -> Int
trd (x:xs) := head (tail xs)
function tail : [Int] -> [Int]
tail (x:[]) := x
tail (x:xs) := xs
function last : [Int] -> Int
last y := case y of
(x:[]) -> x
(x:xs) -> last xs
function lst : [Boolean]
lst := [True,False,True]
function tpl : (Int, Int)
tpl := (1, 2)
function bl : Boolean
bl := True
function tpl2 : (Int, Int) -> Int
tpl2 (1, 2) := 12
tpl2 (x, y) := x-y
tpl2 (3, 4) := 34
function one : (a,b) -> (b,a)
one (x,y) := (y,x)
function pat : Int -> Int -> Int -> Int
pat 0 x 2 := 5
pat 0 1 _ := 12
pat _ _ _ := 14
function patR : Int -> Int -> Int -> Int
patR x 2 5 := x + 1
patR y 2 2 := 2
function grd : Boolean
grd a b := True when a > b
:= False when a < b
:= False
function useDbl : Int -> Int
useDbl x := 4 + dbl x
function dbl : Int -> Int
dbl x := case x of
1 -> 2
x -> 2 * x
function linearError : Int -> Int -> iInt -o Int
linearError x y z := x + y + z + x + y + z
function ca : Int -> Int -> Int -> Int
ca 3 x 2 := case x of
2 -> 2 + x
_ -> 10
ca _ 0 0 := 0
function onePat : Int -> Int
pat 2 := 11
pat _ := 0
function testMap : a
testMap := map (print "x") [1,2,3]
function naturals : [Int]
naturals := cons 0 (map (\x -> x+1) naturals)
-- Sum of first 100 naturals
function main : Int
main := foldr (\x y -> x + y) 0 (take 5 [1,2,3,4,5,6,7,8])
function testPat : Int -> [Int] -> [Int]
testPat := pat 0 dv 2
function lazy : Int
lazy := oneFsc 5 infy
function oneFsc : Int
oneFsc x y := x
function infy : Int
infy := 1 + infy