From 2cd524e55f4d3057fe82db119541b1b5284629dc Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Tue, 4 Oct 2016 20:16:56 +0200 Subject: Examples --- examples/bool.fusp | 2 ++ examples/fold.fusp | 2 +- examples/int.fusp | 10 ++++++++++ examples/list.fusp | 20 ++++++++++++++++++++ examples/palindrome.fusp | 9 +++++++++ 5 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 examples/bool.fusp create mode 100644 examples/int.fusp create mode 100644 examples/palindrome.fusp (limited to 'examples') diff --git a/examples/bool.fusp b/examples/bool.fusp new file mode 100644 index 0000000..2676c6b --- /dev/null +++ b/examples/bool.fusp @@ -0,0 +1,2 @@ +if 0 _ y = y; +if 1 x _ = x; diff --git a/examples/fold.fusp b/examples/fold.fusp index 0d38cdc..a68d671 100644 --- a/examples/fold.fusp +++ b/examples/fold.fusp @@ -11,4 +11,4 @@ faclist n = [n:faclist (sub 1 n)]; fac n = prod (faclist n); -main = fac 12; +main = fac 4; diff --git a/examples/int.fusp b/examples/int.fusp new file mode 100644 index 0000000..fe1e32d --- /dev/null +++ b/examples/int.fusp @@ -0,0 +1,10 @@ +sub a b = code sub a b; +add a b = code add a b; +mul a b = code mul a b; + +eq a b = code eq a b; +ge a b = code ge a b; +gt a b = code gt a b; +le a b = code le a b; +lt a b = code lt a b; +ne a b = code ne a b; diff --git a/examples/list.fusp b/examples/list.fusp index 2fce492..de3059e 100644 --- a/examples/list.fusp +++ b/examples/list.fusp @@ -1,3 +1,5 @@ +import int; + append [] ys = ys; append [x:xs] ys = [x:append xs ys]; @@ -9,3 +11,21 @@ isEmpty _ = 0; hd [x:_] = x; tl [_:x] = x; + +last [x:[]] = x; +last [_:xs] = last xs; + +init [] = []; +init [x:[]] = []; +init [x:xs] = [x:init xs]; + +length_tl i [] = i; +length_tl i [_:xs] = length_tl (add 1 i) xs; + +length xs = length_tl 0 xs; + +repeat 0 _ = []; +repeat n x = [x:repeat (sub 1 n) x]; + +map f [] = []; +map f [x:xs] = [f x:map f xs]; diff --git a/examples/palindrome.fusp b/examples/palindrome.fusp new file mode 100644 index 0000000..d2c6eba --- /dev/null +++ b/examples/palindrome.fusp @@ -0,0 +1,9 @@ +import int; +import list; +import bool; + +is_palindrome [] = 1; +is_palindrome [_:[]] = 1; +is_palindrome [x:xs] = if (eq x (last xs)) (is_palindrome (init xs)) 0; + +main = is_palindrome [0:[42:[37:[42:[0:[]]]]]]; -- cgit v1.2.3