-digitToChar(x){
- x = x % 10;
- if(x == 0){ return '0'; }
- else { if(x == 1){ return '1'; }
- else { if(x == 2){ return '2'; }
- else { if(x == 3){ return '3'; }
- else { if(x == 4){ return '4'; }
- else { if(x == 5){ return '5'; }
- else { if(x == 6){ return '6'; }
- else { if(x == 7){ return '7'; }
- else { if(x == 8){ return '8'; }
- else { return '9'; }}}}}}}}}
-}
-
-printInt (i){
- if(i > 0){
- print(digitToChar(i));
- printInt(i/10);
- }
-}
-
-printIntList (l){
- print('[');
- if(!isEmpty(l)){
- printInt(l.hd);
- l = l.tl;
- }
- while(!isEmpty(l)){
- print(',');
- printInt(l.hd);
- l = l.tl;
- }
- print(']');
-}
-
-printIntListRP(l){
- if(!isEmpty(l)){
- print(',');
- printInt(l.hd);
- printIntListRP(l.tl);
- }
-}
-
-printIntListR(l){
- print('[');
- if(!isEmpty(l)){
- printInt(l.hd);
- l = l.tl;
- }
- printIntListRP(l);
- print("]\n");
-}
-
map(f, l){
if(isEmpty(l)){
return [];
return x + y;
}
-ap(f, x){
- return f(x);
+foldr(f, acc, l){
+ if(isEmpty(l)){
+ return acc;
+ } else {
+ return foldr(f, f(acc, l.hd), l.tl);
+ }
}
main(){
- [Int] l1 = 1 : 2 : [];
- var fun = plus(1);
-
-// var b = ap(fun, 3);
- var res1 = map(fun, l1);
-// var res2 = map(fun, res1);
-
-// printIntListR(l1);
-// printIntListR(res1);
-// printIntListR(res2);
-
-
-
+ [Int] l1 = 1 : 2 : 3 : 4 : 5 : [];
+ var c = foldr(plus, 0, l1);
+ var d = map(plus(1), l1);
}
[Instr "ldl" [Lit t] ""
,Instr "ldh" [Lit 0] "Get function number"
,Instr "str" [Raw "R5"] ""
- ,Instr "bsr" [L "1func"] ""
+ ,Instr "bsr" [L "1func"] "HIGHER ORDER END"
+ ,Instr "ldl" [Lit t] ""
+ ,Instr "ldh" [Lit 1] ""
+ ,Instr "neg" [] ""
+ ,Instr "ldr" [Raw "SP"] ""
+ ,Instr "add" [] ""
+ ,Instr "ldc" [Lit $ length es + 1] ""
+ ,Instr "sub" [] ""
+ ,Instr "str" [Raw "SP"] ""
,Instr "ldr" [Raw "RR"] ""
+ //RR is nu goed
+// ,Instr "ajs" [Lit $ ~ (length es)] ""
+// ,Instr "ldl" [Lit t] ""
+// ,Instr "ldr" [Raw "RR"] "END OF HIGHERORDERFUNCALL"
// ,Instr "ldl" [Lit t] ""
// ,Instr "ldh" [Lit $ 1] ""
// ,Instr "neg" [] ""