Fibonacci recursion -argh

Discussion of Common Lisp

Re: Fibonacci recursion -argh

Postby gugamilare » Sat Oct 24, 2009 5:28 am

speech impediment wrote:I thought this is pretty interesting in the Hyperspec:

Although the order of evaluation of the argument subforms themselves is strictly left-to-right, it is not specified whether the definition of the operator in a function form is looked up before the evaluation of the argument subforms, after the evaluation of the argument subforms, or between the evaluation of any two argument subforms if there is more than one such argument subform. For example, the following might return 23 or 24.

(defun foo (x) (+ x 3))
(defun bar () (setf (symbol-function 'foo) #'(lambda (x) (+ x 4))))
(foo (progn (bar) 20))

I tested this code in both Clozure CL and SBCL and I got 24. It seems that these two implementations look up the definition of the operator after evaluation of the argument subforms. This is why I got confused about this code from Touretzky's book about helping functions.

Code: Select all
(defun count-up (n) (count-up-recursively 1 n)

(defun count-up-recursively (cnt n)
   (cond ((> cnt n) nil)
         (t (cons cnt (count-up-recursively (+ cnt 1) n)))

I just didn't understand how you can use the function count-up-recursively when it wasn't even defined yet.

CL is a dynamic language. You are allowed to define functions later and even to redefine them if you like (except, perhaps, for explicitly inlined functions and ANSI functions). The hyperspec part you quoted is a clear example of that.
Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil

Re: Fibonacci recursion -argh

Postby speech impediment » Sat Oct 24, 2009 11:39 am

Once again, thank you everyone for all your time and help. I am glad that I can move forward. I hope not to be stuck again on another topic. I will probably come back when I hit object orientation. ;)
speech impediment
Posts: 36
Joined: Mon May 04, 2009 5:19 pm


Return to Common Lisp

Who is online

Users browsing this forum: No registered users and 4 guests