From e2e469d84ea5b434697b2ccca49e5b1c2912ef5a Mon Sep 17 00:00:00 2001 From: Samuel Hammersberg Date: Fri, 31 Mar 2023 18:17:28 +0200 Subject: [PATCH] Added some examples that were shown to our handledare. --- sample-programs/example-programs/ex1.crf | 1 + sample-programs/example-programs/ex2.crf | 4 +++ sample-programs/example-programs/ex3.crf | 11 ++++++ sample-programs/example-programs/ex4.crf | 11 ++++++ sample-programs/example-programs/ex5.crf | 26 ++++++++++++++ sample-programs/example-programs/ex6.crf | 43 ++++++++++++++++++++++++ 6 files changed, 96 insertions(+) create mode 100644 sample-programs/example-programs/ex1.crf create mode 100644 sample-programs/example-programs/ex2.crf create mode 100644 sample-programs/example-programs/ex3.crf create mode 100644 sample-programs/example-programs/ex4.crf create mode 100644 sample-programs/example-programs/ex5.crf create mode 100644 sample-programs/example-programs/ex6.crf diff --git a/sample-programs/example-programs/ex1.crf b/sample-programs/example-programs/ex1.crf new file mode 100644 index 0000000..c7ad3b2 --- /dev/null +++ b/sample-programs/example-programs/ex1.crf @@ -0,0 +1 @@ +main = 5 + 2; \ No newline at end of file diff --git a/sample-programs/example-programs/ex2.crf b/sample-programs/example-programs/ex2.crf new file mode 100644 index 0000000..3510463 --- /dev/null +++ b/sample-programs/example-programs/ex2.crf @@ -0,0 +1,4 @@ +main = case 78 of { + 5 => 45; + x => x + 24; +}; \ No newline at end of file diff --git a/sample-programs/example-programs/ex3.crf b/sample-programs/example-programs/ex3.crf new file mode 100644 index 0000000..408e685 --- /dev/null +++ b/sample-programs/example-programs/ex3.crf @@ -0,0 +1,11 @@ +data Maybe () where { + Just : Int -> Maybe () + Nothing : Maybe () +}; + +demoFunc x = case x of { + Just x => x + 24; + Nothing => 0; +}; + +main = demoFunc (Just 5) ; \ No newline at end of file diff --git a/sample-programs/example-programs/ex4.crf b/sample-programs/example-programs/ex4.crf new file mode 100644 index 0000000..a64adb5 --- /dev/null +++ b/sample-programs/example-programs/ex4.crf @@ -0,0 +1,11 @@ +data Maybe () where { + Just : Int -> Maybe () + Nothing : Maybe () +}; + +demoFunc x = case x of { + Just x => x + 24; + Nothing => 0; +}; + +main = demoFunc Nothing ; \ No newline at end of file diff --git a/sample-programs/example-programs/ex5.crf b/sample-programs/example-programs/ex5.crf new file mode 100644 index 0000000..b9457ed --- /dev/null +++ b/sample-programs/example-programs/ex5.crf @@ -0,0 +1,26 @@ +main = case f (Just 10) of { + Just a => a ; + Nothing => 0 ; +}; + +f x = bind (fmap (\s . s + 1) x) (\s . pure (s + 10)) ; + +data Maybe () where { + Just : Int -> Maybe () + Nothing : Maybe () +}; + +fmap : (Int -> Int) -> Maybe () -> Maybe () ; +fmap f m = case m of { + Just a => pure (f a) ; + Nothing => Nothing ; +}; + +pure : Int -> Maybe () ; +pure x = Just x; + +bind : Maybe () -> (Int -> Maybe ()) -> Maybe () ; +bind x f = case x of { + Just x => f x ; + Nothing => Nothing ; +}; \ No newline at end of file diff --git a/sample-programs/example-programs/ex6.crf b/sample-programs/example-programs/ex6.crf new file mode 100644 index 0000000..41894a0 --- /dev/null +++ b/sample-programs/example-programs/ex6.crf @@ -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; \ No newline at end of file