(define ...) inside a default value
Posted: Fri Jul 25, 2014 4:31 am
I have no reason to use something like that in real code, so this is just pure curiosity. Nevertheless, I would like to understand why this piece of code does what it does.
In newLisp, the following code
will print AnilD. I don't quite understand where nil comes from. I would expect either ABD or, less intuitive but still understandable, ABC. It gets even more confusing. When I do
the interpreter tells me:
And, indeed, in this case I have to restart the shell to make it work again.
In Common Lisp, Clojure and Ruby the following pieces of code seem to be equivalent:
All of them print ABD.
In newLisp, the following code
Code: Select all
(define (f a (b (define (f a) "D"))) a)
(println (f "A" 1) (f "B") (f "C"))
Code: Select all
(define (f a (b (define (f a) "D"))) (println "") a)
(f "A")
Code: Select all
--- cannot execute: restart shell ---
In Common Lisp, Clojure and Ruby the following pieces of code seem to be equivalent:
Code: Select all
(defun f (a &optional (b (defun f (a) "D"))) a)
(format t "~a~a~a" (f "A" 1) (f "B") (f "C"))
Code: Select all
(defn f ([a] (defn f [a] "D") a) ([a b] a))
(println (str (f "A" 1) (f "B") (f "C")))
Code: Select all
def f(a, b = def f(a); "D"; end); a; end
p f("A", 1) + f("B") + f("C")