2 Three ways to implement the f acto rial function in SPL.
3 First the recursive version .
9 facR(n) :: Int -> Int {
18 //The iterative version of the factorial function
19 facl ( n ) :: Int -> Int {
28 //A main function to check the results
29 //It takes no arguments, so the type looks like this:
36 if (facN != factl (n) || facn != facL (n)){
37 print (n : facN : facl (n) : facL (n): []);
44 // A list based factorial function
45 // Defined here to show that functions can be given in any order (unlike C)
46 facL (n) :: Int -> Int {
47 return product (fromTo(1, n) ); //Inline comments yay
50 //Generates a list of integers from the first to the last argument
51 fromTo (from, to) :: Int -> Int -> [Int] {
53 return from:fromTo(from+1, to);
59 //Make a reversed copy of any list
60 reverse(list):: [t] -> [t] {
62 while(!isEmpty(list)){
69 //Absolute value, in a strange layout
70 abs(n)::Int->Int{if(n<0)return -n; else return n;}
72 //swap the elements in a tuple
73 swap(tuple) :: (a, a) -> (a, a){
75 tuple.fst = tuple.snd;
81 append(l1, l2) :: [t] -> [t] -> [t] {
85 l1.tl = append(l1.tl, l2);
90 //square the odd numbers in a list and remove the even members
91 squareOddNumbers(list) :: [Int] -> [Int] {
92 while(!isEmpty (list) && list.hd % 2==0){
96 list.hd = list.hd*list.hd;
97 list.tl = squareOddNumbers(list.tl);
101 //deze comment eindigt met EOF ipv newline