diff options
author | kartofen <mladenovnasko0@gmail.com> | 2024-09-02 00:47:14 +0300 |
---|---|---|
committer | kartofen <mladenovnasko0@gmail.com> | 2024-09-02 00:47:14 +0300 |
commit | c9dddfa463d25f3af4dee5d20fe3eaeb23aed567 (patch) | |
tree | 11d76dc034bae308b17c40bc69ff183b25605feb | |
parent | 6def1ae9744b0f56ee04d05db622f4b2a8ca96e9 (diff) |
cooler macro example
-rw-r--r-- | files/test-lambda.l | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/files/test-lambda.l b/files/test-lambda.l index b29ed7b..29e1f52 100644 --- a/files/test-lambda.l +++ b/files/test-lambda.l @@ -54,8 +54,6 @@ (defmacro m (a) `(+ ,a 57)) (m (+ 1 2)) -(define cadr (lambda (l) (car (cdr l)))) - (define map (lambda (fn list) (if (nil? list) list @@ -63,7 +61,27 @@ (map (lambda (n) (+ n 1)) '(1 2 3)) +(define foldl (lambda (fn acc l) + (if (nil? l) acc + (foldl fn (fn (car l) acc) (cdr l))))) +(define foldr (lambda (fn acc l) + (if (nil? l) acc + (fn (car l) (foldr fn acc (cdr l)))))) + + +(define cdar (lambda (l) (car (cdr l)))) + (defmacro let (vars body) - `((lambda ,(map car vars) ,body) . ,(map cadr vars))) + `((lambda ,(map car vars) ,body) . ,(map cdar vars))) + +(defmacro let* (vars body) + (foldr (lambda (var rest) `(let (,var) ,rest)) body vars)) + +(let ((a 9) + (b 13)) + (+ a b)) -(let ((a 8) (b 9)) (+ a b)) +(let* ((a 8) + (b (- a 3)) + (c (+ a b))) + (- c 3)) |