Commit ca799ed1 authored by Pierre-Antoine Rouby's avatar Pierre-Antoine Rouby 🤘🏻
Browse files

feat: Update let macros.

parent ce546e05
Pipeline #89668 passed with stages
in 34 seconds
......@@ -6,21 +6,24 @@ Hylang Truelisp
## Features
| Name | Description | Type |
|-------------|---------------------------------|----------|
| λ | Define lambda function | Macro |
| lambda | Define lambda function | Macro |
| car! | Return first element of list | Marco |
| cdr! | Return rest of list | Macro |
| car | Return first element of list | Function |
| cdr | Return rest of list | Function |
| append | Concat 2 list | Function |
| list? | True if arguement is an list | Function |
| map-list | map l with function f | Function |
| reverse | Reverse list | Function |
| remove-last | Remove last element of list | Function |
| cons* | cons multiple element with list | Macro |
| let | Define local variable | Macro |
| Name | Description | Type |
|-------------|---------------------------------------|----------|
| λ | Define lambda function | Macro |
| lambda | Define lambda function | Macro |
| car! | Return first element of list | Marco |
| cdr! | Return rest of list | Macro |
| car | Return first element of list | Function |
| cdr | Return rest of list | Function |
| append | Concat 2 list | Function |
| list? | True if arguement is an list | Function |
| map-list | map l with function f | Function |
| reverse | Reverse list | Function |
| remove-last | Remove last element of list | Function |
| cons* | cons multiple element with list | Macro |
| setl | Define variable from list [key value] | Macro |
| let1 | Define one local variable | Macro |
| let | Define locals variables | Macro |
## Examples
......@@ -41,7 +44,7 @@ Hylang Truelisp
```hy
(require [truelisp [*]])
(let foo 4
(let bar 10
(+ 2 (* foo bar))))
(let [[foo 4]
[bar 10]]
(+ 2 (* foo bar)))
```
......@@ -16,8 +16,8 @@
(defmain [&rest args]
(when (< (len args) 2) (usage args))
(let f (λ [x]
(do
(print "Calcul fibonacci of" x)
(print (fibo (int x)))))
(map-list f (cdr args))))
(let1 [f (λ [x]
(do
(print "Calcul fibonacci of" x)
(print (fibo (int x)))))]
(map-list f (cdr args))))
......@@ -86,14 +86,35 @@
(test "(cons* 1 2 3 [4 5])"
(= [1 2 3 4 5] (cons* 1 2 3 [4 5])))
;;;
;;; Setl.
;;;
(setl [setlvar 42])
(test "(setl [setlvar 42])"
(= 42 setlvar))
;;;
;;; Let.
;;;
(test "(let foo 42 ...)"
(= 42 (let foo 42 foo)))
(test "(let1 [foo 42] foo)"
(= 42 (let1 [foo 42] foo)))
(test "(let foo 22 (let bar 20 (+ foo bar)))"
(= 42 (let foo 22
(let bar 20
(test "(let1 [foo 22] (let [bar 20] (+ foo bar)))"
(= 42 (let1 [foo 22]
(let1 [bar 20]
(+ foo bar)))))
(test "(let [[var1 foo] [var2 bar]] (+ var1 var2)"
(= "foobar" (let [[var1 "foo"] [var2 "bar"]]
(+ var1 var2))))
(test "(let ... (ax^2 + bx + c))"
(= 98 (let [[a 5]
[b 2]
[c 10]
[x 4]]
(+ (* a (* x x))
(* b x)
c))))
......@@ -66,7 +66,17 @@
;;; Let.
;;;
(defmacro let [vname value body]
(defmacro setl [[var value]]
`(setv ~var ~value))
(defmacro let1 [var body]
`(do
(setv ~vname ~value)
(setl ~var)
~body))
(defmacro let [vars body]
(if (empty? vars)
`~body
`(let1 ~(car! vars)
(let ~(cdr! vars)
~body))))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment