2 Three ways to implement the f acto rial function in SPL.
3 First the recursive version .
14 facR(n) :: Int -> Int {
23 //The iterative version of the factorial function
24 facl ( n ) :: Int -> Int {
33 //A main function to check the results
34 //It takes no arguments, so the type looks like this:
41 if (facN != factl (n) || facn != facL (n)){
42 print (n : facN : facl (n) : facL (n): []);
49 // A list based factorial function
50 // Defined here to show that functions can be given in any order (unlike C)
51 facL (n) :: Int -> Int {
52 return product (fromTo(1, n) ); //Inline comments yay
55 //Generates a list of integers from the first to the last argument
56 fromTo (from, to) :: Int -> Int -> [Int] {
58 return from:fromTo(from+1, to);
64 //Make a reversed copy of any list
65 reverse(list):: [t] -> [t] {
67 while(!isEmpty(list)){
74 //Absolute value, in a strange layout
75 abs(n)::Int->Int{if(n<0)return -n; else return n;}
77 //swap the elements in a tuple
78 swap(tuple) :: (a, a) -> (a, a){
80 tuple.fst = tuple.snd;
86 append(l1, l2) :: [t] -> [t] -> [t] {
90 l1.tl = append(l1.tl, l2);
95 //square the odd numbers in a list and remove the even members
96 squareOddNumbers(list) :: [Int] -> [Int] {
97 while(!isEmpty (list) && list.hd % 2==0){
101 list.hd = list.hd*list.hd;
102 list.tl = squareOddNumbers(list.tl);
106 //deze comment eindigt met EOF ipv newline