Added some examples that were shown to our handledare.
This commit is contained in:
parent
b0ec5a2333
commit
e2e469d84e
6 changed files with 96 additions and 0 deletions
43
sample-programs/example-programs/ex6.crf
Normal file
43
sample-programs/example-programs/ex6.crf
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
main = sum (repeat (sumlength (repeat 10 2000)) 5);
|
||||
|
||||
-- a simple list data type containing ints
|
||||
data List () where {
|
||||
Cons : Int -> List () -> List ()
|
||||
Nil : List ()
|
||||
};
|
||||
|
||||
-- take the length of a list
|
||||
length : List () -> Int ;
|
||||
length x = case x of {
|
||||
Cons _ xs => 1 + length xs ;
|
||||
Nil => 0 ;
|
||||
};
|
||||
-- sum a list
|
||||
sum : List () -> Int ;
|
||||
sum x = case x of {
|
||||
Cons a xs => a + sum xs ;
|
||||
Nil => 0 ;
|
||||
};
|
||||
|
||||
-- sum + length of a list
|
||||
sumlength: List () -> Int ;
|
||||
sumlength x = sum x + length x ;
|
||||
|
||||
-- take the head of a list
|
||||
head : List () -> Int ;
|
||||
head x = case x of {
|
||||
Cons h _ => h ;
|
||||
};
|
||||
|
||||
-- repeat an element n times
|
||||
repeat : Int -> Int -> List () ;
|
||||
repeat x n = repeatHelp Nil x n;
|
||||
repeatHelp : List () -> Int -> Int -> List () ;
|
||||
repeatHelp acc x n = case n of {
|
||||
0 => acc ;
|
||||
n => repeatHelp (Cons x acc) x (n + minusOne) ;
|
||||
};
|
||||
|
||||
-- represents minus one :)
|
||||
minusOne : Int ;
|
||||
minusOne = 9223372036854775807 + 9223372036854775807 + 1;
|
||||
Loading…
Add table
Add a link
Reference in a new issue