Search found 5 matches

by MaartenM
Wed Jul 16, 2008 9:43 am
Forum: Common Lisp
Topic: closures
Replies: 6
Views: 17545

closures

I am just learning Lisp. My first reaction when I saw this..: ? (let ((counter 0)) (defun counter-next () (incf counter)) (defun counter-reset () (setq counter 0))) COUNTER-RESET ? (counter-next) 1 ? (counter-next) 2 ? (counter-next) 3 ? (counter-next) 4 ? (counter-reset) 0 ? (counter-next) 1 ..was ...
by MaartenM
Tue Jul 15, 2008 2:34 pm
Forum: Common Lisp
Topic: question about recursion (easy)
Replies: 8
Views: 22509

Re: question about recursion (easy)

Thanks guys, for the examples.
I now understand better how to implement left-hand side in Lisp with pure recursion instead of loops.
by MaartenM
Tue Jul 15, 2008 9:27 am
Forum: Common Lisp
Topic: question about recursion (easy)
Replies: 8
Views: 22509

Re: question about recursion (easy)

But this recalculates a large number of recursive branches redundantly, doesn't it? Or is there some magic goign on there I'm not aware of?
by MaartenM
Tue Jul 15, 2008 8:59 am
Forum: Common Lisp
Topic: question about recursion (easy)
Replies: 8
Views: 22509

Re: question about recursion (easy)

Ok, I managed to do it with the 'dotimes' macro:

Code: Select all

(defun cseries-list (n a)
  (let ((l (list a)))
    (dotimes (i n)
       (if (even i)
	  (push (log (car l)) l)
	  (push (square (car l)) l)))
    (nreverse l)))
Is this the 'Lisp' way?
by MaartenM
Tue Jul 15, 2008 8:08 am
Forum: Common Lisp
Topic: question about recursion (easy)
Replies: 8
Views: 22509

question about recursion (easy)

I tried my first LISP today. I got stuck trying to build a list of all intermediate values of the function 'c-series': (defun even (n) (= (mod n 2) 0)) (defun square (x) (* x x)) (defun c-series (n a) (if (= n 0) a (if (even n) (log (c-series (- n 1) a)) (square (c-series (- n 1) a)) ) ) ) What if I...