Menu

Show posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.

Show posts Menu

Messages - Zabolekar

#1
Thanks.
#2
Thanks for the answer! So the old function definition is collected right after we redefine the function? That makes sense for me. Then, as far as I understand, modifying the function without actually redefining it should be safe (is it?):


(define (f a (b (setf (nth 1 f) "D"))) a)
(println (f "A" 1) (f "B") (f "C"))


It  prints ADD, as expected.



Is there a way to self-modify a function so that the changes only take place after the function returns?
#3
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


(define (f a (b (define (f a) "D"))) a)
(println (f "A" 1) (f "B") (f "C"))


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


(define (f a (b (define (f a) "D"))) (println "") a)
(f "A")


the interpreter tells me:


--- cannot execute: restart shell ---

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:


(defun f (a &optional (b (defun f (a) "D"))) a)
(format t "~a~a~a" (f "A" 1) (f "B") (f "C"))


(defn f ([a] (defn f [a] "D") a) ([a b] a))
(println (str (f "A" 1) (f "B") (f "C")))


def f(a, b = def f(a); "D"; end); a; end
p f("A", 1) + f("B") + f("C")


All of them print ABD.