Lisp programs contain nested function call;
For example: (let results '((ex1 (1 0 1)) (ex2 (1 1 1)) (ex3 (0.5 0.7 1)))
(/ (reduce + (alref results 'ex1)) 3))
There are three problems with this
style:
The first problem is that it is a one liner
and some times they hide nasty bugs,
The second is that it is in a reverse order,
The last problem is that it is hard to debug.So here is my a code that create an Anaphoric Local Scope: (def split@@ (lst (o acc))
(if (empty lst) (list (rev acc))
(caris lst '@) (cons (rev acc) (split@@ (cdr lst)))
(caris lst '@@) (cons (cons 'binding-label (rev acc))
(split@@ (cdr lst)))
(split@@ (cdr lst) (cons (car lst) acc))))
(def _ascope (lst)
(if (empty lst) ()
(with (e (car lst) r (cdr lst))
(if (caris e 'binding-label)
(list (join (cdr e) (_ascope r)))
(caris e 'dbg)
(cons (cdr e) (_ascope r))
(if (empty r) (list e)
(cons `(= it ,e) (_ascope r)))))))
(mac ascope lst `(let it nil ,@(_ascope (split@@ lst))))
And here is the example using the anaphoric scope: (ascope
let results '((ex1 (1 0 1)) (ex2 (1 1 1)) (ex3 (0.5 0.7 1))) @@
alref results 'ex1 @
dbg prn it @
reduce + it @
dbg prn it @
/ it 3 @
dbg prn it)
It is longer but I think that it is safer
and more readable.An example of how one liners can hide
nasty bugs:
http://arclanguage.org/item?id=11556 What do you think? |