--- /dev/null
+map(f, l){
+ if(isEmpty(l)){
+ return [];
+ } else {
+ return f(l.hd) : map(f, l.tl);
+ }
+}
+
+foldr(f, acc, l){
+ if(isEmpty(l)){
+ return acc;
+ } else {
+ return foldr(f, f(acc, l.hd), l.tl);
+ }
+}
+
+filter(f, l){
+ if(isEmpty(l)){
+ return [];
+ } else {
+ if(f(l.hd)){
+ return filter(f, l.tl);
+ } else {
+ return l.hd : filter(f, l.tl);
+ }
+ }
+}
+
+intList(x){
+ [Int] l = [];
+ Int a = 1;
+ while(a <= x){
+ l = a : l;
+ a = a + 1;
+ }
+ return l;
+}
+
+main(){
+ print(
+ "faculty of 5 is: ",
+ foldr(\x y->x*y, 1, intList(z)),
+ "sum of 1..5 is: ",
+ foldr(\x y->x+y, 0, intList(z)),
+ "filter evens from 0..12 is: ",
+ filter(\x->x % 2 == 0, intList(12)));
+}
--- /dev/null
+//Classical higher order functions:)
+map(f, l){
+ if(isEmpty(l)){
+ return [];
+ } else {
+ return f(l.hd) : map(f, l.tl);
+ }
+}
+
+foldr(f, acc, l){
+ if(isEmpty(l)){
+ return acc;
+ } else {
+ return foldr(f, f(acc, l.hd), l.tl);
+ }
+}
+
+//Functions for operators
+plus(x, y){ return x + y; }
+times(x, y){ return x * y; }
+
+intList(x){
+ [Int] l = [];
+ Int a = 1;
+ while(a <= x){
+ l = a : l;
+ a = a + 1;
+ }
+ return l;
+}
+
+faculty(x){
+ return foldr(times, 1, intList(x));
+}
+
+sum(x){
+ return foldr(plus, 0, intList(x));
+}
+
+main(){
+ print("faculty of 5 is: ", faculty(5));
+ print("sum of 1..5 is: ", sum(50));
+}
return x * y;
}
+intList(x){
+ [Int] l = [];
+ Int a = 1;
+ while(a <= x){
+ l = a : l;
+ a = a + 1;
+ }
+ return l;
+}
+
+faculty(x){
+ return foldr(times, 1, intList(x));
+}
+
main(){
[Int] l1 = 1 : 2 : 3 : 4 : 5 : [];
- foldr(times, 1, l1);
print("faculty of 5 is: ");
- print(foldr(times, 1, l1));
+ print(faculty(5));
}
--- /dev/null
+hyper(n, a, b){
+ if(n == 0){ return b + 1;
+ } else { if(b == 0 && n == 1){ return a;
+ } else { if(b == 0 && n == 2){ return 0;
+ } else { if(b == 0 && n >= 3){ return 1;
+ } else { return hyper(n-1, a, hyper(n, a, b - 1));
+ }}}}
+}
+
+main(){
+ var plus = hyper(2);
+ var times = hyper(3);
+ var power = hyper(4);
+ var arrow = hyper(5);
+
+ print("2 + 4 = ", plus(2, 4));
+ print("2 * 4 = ", times(2, 4));
+ print("2 ^ 4 = ", power(2, 4));
+ print("3 | 2 = ", arrow(3, 2));
+}
(parseBBraces $ parseSepList CommaToken parseIdent)
(optional (satTok DoubleColonToken *> parseFunType))
(satTok CBraceOpenToken *> many parseVarDecl)
- (many parseStmt <* satTok CBraceCloseToken)
+ (flatten <$> (many parseStmt <* satTok CBraceCloseToken))
-parseStmt :: Parser Token Stmt
-parseStmt = parseIfStmt <|> parseWhileStmt <|>
+parseStmt :: Parser Token [Stmt]
+parseStmt = (parseIfStmt <|> parseWhileStmt <|>
parseSColon parseAssStmt <|> parseSColon parseReturnStmt <|>
(parseSColon parseFunCall
- >>= \(ident, args, fs)->pure $ FunStmt ident args fs)
+ >>= \(ident, args, fs)->pure $ FunStmt ident args fs))
+ >>= \stmt->case stmt of
+ FunStmt "print" args fs = pure $ map (\a->FunStmt "print" [a] []) args
+ s = pure [s]
where
parseSColon :: (Parser Token a) -> Parser Token a
parseSColon p = p <* satTok SColonToken
(parseBBraces parseExpr) <*> (parseBlock <|> parseOneLine))
parseBlock :: Parser Token [Stmt]
- parseBlock = parseBCBraces (many parseStmt)
+ parseBlock = parseBCBraces (flatten <$> many parseStmt)
parseOneLine :: Parser Token [Stmt]
- parseOneLine = pure <$> parseStmt
+ parseOneLine = parseStmt
parseFunType :: Parser Token Type
parseFunType = parseFT >>= \t -> case t of