lib/lasp/stdmacros.lasp in lasp-0.8.0 vs lib/lasp/stdmacros.lasp in lasp-0.9.0
- old
+ new
@@ -6,18 +6,49 @@
(def defm
(macro (name params body)
(list 'def name
(list 'macro params body))))
+
; Shorthand for defining functions
;
; (defn f (x) (+ x 1))
; expands to:
; (def f (fn (x) (do (+ x 1))))
(defm defn
(name params & body)
(list 'def name
(list 'fn params (cons 'do body))))
+
+
+; Creates local bindings.
+;
+; It does this by (ab)using closures. It rewrites the form as a recursively
+; nested tree of functions, declaring parameters and instantly executing the
+; function passing in corresponding arguments. This should be considered an
+; implementation detail, let can (and should) be used without knowledge or
+; regard to that it creates closures behind the scenes.
+
+; Example:
+; (let (one 1
+; two (+ one 1)) ; Previous bindings are available
+; (+ one two))
+; ; => 3
+;
+; expands to:
+; ((fn (one) ((fn (two) (do (+ one two))) (+ one 1))) 1)
+(defn let*
+ (bindings body)
+ (if (empty? bindings)
+ (cons 'do body)
+ (list
+ (list 'fn (list (first bindings)) (let* (drop 2 bindings) body))
+ (second bindings))))
+
+(defm let
+ (bindings & body)
+ (let* bindings body))
+
; See unevaluated form the macro expands to for debugging macros
;
; This simply uses the fact that `apply` already has this effect
; of expanding macros and makes the syntax a bit nicer, you can