summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2016-11-20 22:41:42 +0100
committertomsmeding <tom.smeding@gmail.com>2016-11-20 22:41:42 +0100
commitbe1d4b5da9c7e8b36b355ef0df58cee94543b74c (patch)
tree904c014e252a79375831ad8f571aba09dfa7fdee
parent83bdc306f781204dfb4094247932d13cc40edded (diff)
Add `init` and `cons`
-rw-r--r--prelude.cpp10
1 files changed, 9 insertions, 1 deletions
diff --git a/prelude.cpp b/prelude.cpp
index 8fc9d8a..7d9b4c3 100644
--- a/prelude.cpp
+++ b/prelude.cpp
@@ -19,7 +19,8 @@ const AST afterBootstrap=AST(R"RAW(
(def 'print (. putstr repr))
(def '$ \f \x (f x))
(def 'if \c \t \f (__if c (() t) (() f)))
- (def 'last \l (if (nil (tail l)) (head l) (last (tail l)))))
+ (def 'last \l (if (nil (tail l)) (head l) (last (tail l))))
+ (def 'init \l (if (nil (tail l)) '() (cons (head l) (init (tail l))))))
)RAW");
static AST dofunction(const AST&);
@@ -81,6 +82,13 @@ public:
return res;
}));
+ intoEnv.define("cons",checkedHook("cons",{},{AST::Type::tuple},
+ [](Environment&,const AST &arg1,const AST &arg2) -> AST {
+ AST res(arg2);
+ res.terms.insert(res.terms.begin(),arg1);
+ return res;
+ }));
+
intoEnv.define("nil",checkedHook("nil",{AST::Type::tuple},
[](Environment&,const AST &arg) -> AST {
return AST::makeNumber(arg.terms.size()==0);