Three ways to implement the f acto rial function in SPL.
First the recursive version .
*/
+
facR(n) :: Int -> Int {
if (n < 2) {
return 1;
}
}
+
//The iterative version of the factorial function
facl ( n ) :: Int -> Int {
var r = 1;
var ok = True;
while(n<20) {
facN = facR (n);
- if (facN != factl (n) || facn != facL (n)){
+ if (facN != factl (n) || facN != facL (n)){
print (n : facN : facl (n) : facL (n): []);
ok=False;
}
}
- print(ok);
+ //print(ok);
}
// A list based factorial function
}
//Generates a list of integers from the first to the last argument
-fromTo (from, to) :: Int Int -> [Int] {
+fromTo (from, to) :: Int -> Int -> [Int] {
if(from <= to){
return from:fromTo(from+1, to);
} else {
abs(n)::Int->Int{if(n<0)return -n; else return n;}
//swap the elements in a tuple
-swap(tuple) :: (a, a) -> (a, a){
- var tmp = tuple.fst;
- tuple.fst = tuple.snd;
- tuple.snd = tmp;
- return tuple;
-}
+//swap(tuple) :: (a, a) -> (a, a){
+// var tmp = tuple.fst;
+// tuple.fst = tuple.snd;
+// tuple.snd = tmp;
+// return tuple;
+//}
//list append
-append(l1, l2) :: [t] [t] -> [t] {
+append(l1, l2) :: [t] -> [t] -> [t] {
if(isEmpty(l1)){
return l2;
} else {
//square the odd numbers in a list and remove the even members
squareOddNumbers(list) :: [Int] -> [Int] {
- while(!isEmpty (list) && list.hd % 2=0){
+ while(!isEmpty (list) && list.hd % 2==0){
list=list.tl;
}
if(!isEmpty(list)){