2 Three ways to implement the f acto rial function in SPL.
3 First the recursive version .
6 facR(n) :: Int -> Int {
15 //The iterative version of the factorial function
16 facl ( n ) :: Int -> Int {
25 //A main function to check the results
26 //It takes no arguments, so the type looks like this:
33 if (facN != factl (n) || facN != facL (n)){
34 print (n : facN : facl (n) : facL (n): []);
41 // A list based factorial function
42 // Defined here to show that functions can be given in any order (unlike C)
43 facL (n) :: Int -> Int {
44 return product (fromTo(1, n) ); //Inline comments yay
47 //Generates a list of integers from the first to the last argument
48 fromTo (from, to) :: Int -> Int -> [Int] {
50 return from:fromTo(from+1, to);
56 //Make a reversed copy of any list
57 reverse(list):: [t] -> [t] {
59 while(!isEmpty(list)){
66 //Absolute value, in a strange layout
67 abs(n)::Int->Int{if(n<0)return -n; else return n;}
69 //swap the elements in a tuple
70 //swap(tuple) :: (a, a) -> (a, a){
71 // var tmp = tuple.fst;
72 // tuple.fst = tuple.snd;
78 append(l1, l2) :: [t] -> [t] -> [t] {
82 l1.tl = append(l1.tl, l2);
87 //square the odd numbers in a list and remove the even members
88 squareOddNumbers(list) :: [Int] -> [Int] {
89 while(!isEmpty (list) && list.hd % 2==0){
93 list.hd = list.hd*list.hd;
94 list.tl = squareOddNumbers(list.tl);
98 //deze comment eindigt met EOF ipv newline