Search found 6 matches
- Sun Oct 30, 2011 2:06 am
- Forum: Common Lisp
- Topic: help understanding a simple function
- Replies: 23
- Views: 38915
Re: help understanding a simple function
... (defun numbers (lst) (setq new-lst '()) (cond ((numberp (car lst)) (cons (car lst) new-lst) (numbers (cdr lst))) ((null lst) new-lst) (t (numbers (cdr lst))))) Actually, to solve the task recursively, I'ld propose a solution quite similar to your attempt above. You just don't need this new-lst v...
- Sat Oct 29, 2011 12:19 am
- Forum: Common Lisp
- Topic: help understanding a simple function
- Replies: 23
- Views: 38915
Re: help understanding a simple function
For simlicity, the task was defined to work with not-empty lists only. So very good! By the way, though cond returns nil by default, I think it's convention to always state the 'fall-through'-case in cond, like so: (defun numbersp (lst) (cond ((null lst) T) ((numberp (car lst)) (numbersp (cdr lst)))...
- Fri Oct 28, 2011 1:11 am
- Forum: Common Lisp
- Topic: help understanding a simple function
- Replies: 23
- Views: 38915
Re: help understanding a simple function
Ok, you're on the right way, but: First, think about the cases you need to express. Though we defined the incoming list to be not nil, we'll find nil once we looked at each element (and found only numbers). We want to use the two basic cases when working with lists: If the car is nil, return ... Els...
- Wed Oct 26, 2011 1:57 am
- Forum: Common Lisp
- Topic: help understanding a simple function
- Replies: 23
- Views: 38915
Re: help understanding a simple function
In general, you can write an iterative algorithm to substitute a recursive and vice versa.
It's up to you...
But if you want to learn about recursion the answer is 'yes, you need to!'
It's up to you...
But if you want to learn about recursion the answer is 'yes, you need to!'

- Mon Oct 24, 2011 10:35 am
- Forum: Common Lisp
- Topic: help understanding a simple function
- Replies: 23
- Views: 38915
Re: help understanding a simple function
If hope the sum function above is ok. Very good! It can be hard to wrap one's mind around some recursive functions, and since you're learning from Barski (like I did and do) you'll want to have a feel for it when it comes to 'dice of doom' :) If you can afford it and like to improve yourself one li...
- Thu Oct 20, 2011 11:38 am
- Forum: Common Lisp
- Topic: help understanding a simple function
- Replies: 23
- Views: 38915
Re: help understanding a simple function
Well, how about a function adding 1 to each element of a list (assuming it contains only numbers): (defun add-1 (lst) (if lst (cons (1+ (car lst)) (add-1 (cdr lst))) nil)) The key point that helped me understand recursion is not to try to follow the recursion. If you recur on a list, you (well, basi...