Search found 15 matches
- Fri Nov 18, 2011 3:25 am
- Forum: Common Lisp
- Topic: help understanding a simple function
- Replies: 23
- Views: 38899
Re: help understanding a simple function
I thought I'd post a quick update. Once again, thank you everyone for the help so far. I haven't posted anything in a while. I haven't forgotten about this thread:) The only reason for that is that I've got a very busy period at work and have to bring a lot of paperwork home so don't really have tim...
- Sat Oct 29, 2011 8:20 am
- Forum: Common Lisp
- Topic: help understanding a simple function
- Replies: 23
- Views: 38899
Re: help understanding a simple function
Ok, I think I got another one. The problem is that it is NOT done recursively - I've got a sneaking suspicion that's not the way it's meant to be solved. Extract the numbers! (defun numbers (lst) ...) > (numbers '(1 2 a b 3 c 4 1)) => (1 2 3 4 1) (defun numbers (lst) (labels ((is-number (elt) (numbe...
- Sat Oct 29, 2011 6:33 am
- Forum: Common Lisp
- Topic: help understanding a simple function
- Replies: 23
- Views: 38899
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 2:45 pm
- Forum: Common Lisp
- Topic: help understanding a simple function
- Replies: 23
- Views: 38899
Re: help understanding a simple function
Thanks a lot. The moment I read your post I realised my mistake. Of course, once the list is empty, it evaluates to NIL. (defun numbersp (lst) (cond ((null lst) T) ((numberp (car lst)) (numbersp (cdr lst))))) It seems to work fine. Is that correct? Time to look at another of the tasks.
- Wed Oct 26, 2011 12:44 pm
- Forum: Common Lisp
- Topic: help understanding a simple function
- Replies: 23
- Views: 38899
Re: help understanding a simple function
hmm, I must say it's not easy for me. Let's start with: Check if a list contains only numbers (assume it's not empty). First I wrote the following: CL-USER> (defun numbersp (lst) (cond ((numberp (car lst)) 'car-is-a-number) (nil))) NUMBERSP CL-USER> (numbersp '(1 2 3)) CAR-IS-A-NUMBER CL-USER> (numb...
- Tue Oct 25, 2011 12:06 pm
- Forum: Common Lisp
- Topic: help understanding a simple function
- Replies: 23
- Views: 38899
Re: help understanding a simple function
Excellent. Thank you. I do appreciate it.
Just one question. Do I need to use recursion in each of the tasks?
Just one question. Do I need to use recursion in each of the tasks?
- Sat Oct 22, 2011 5:06 am
- Forum: Common Lisp
- Topic: help understanding a simple function
- Replies: 23
- Views: 38899
Re: help understanding a simple function
If hope the sum function above is ok. Would it be too much if I asked you to give me a couple of tasks that would test my understanding of recursion? I thought it'd be better if you guys set a task for me, because as I am a LISP newbie, unknowingly I might come up with some tasks that may require de...
- Sat Oct 22, 2011 1:45 am
- Forum: Common Lisp
- Topic: help understanding a simple function
- Replies: 23
- Views: 38899
Re: help understanding a simple function
Thank you for your post. The key point that helped me understand recursion is not to try to follow the recursion. Is it possible? LOL. I involuntarily do it which leads me to madness. Try to write a function sum that sums all elements in a list! I think it should be as follows: (defun sum-of-numbers...
- Thu Oct 20, 2011 4:50 am
- Forum: Common Lisp
- Topic: help understanding a simple function
- Replies: 23
- Views: 38899
Re: help understanding a simple function
It's not adding numbers and strings. When it calls (1+ (my-length '(with four symbols))), it's adding 1 to whatever (my-length '(with four symbols)) returns. That's not a string, it's a number. It calls (my-length '(four symbols)), which calls (my-length '(symbols)), which calls (my-length '()), wh...
- Wed Oct 19, 2011 2:50 pm
- Forum: Common Lisp
- Topic: help understanding a simple function
- Replies: 23
- Views: 38899
Re: help understanding a simple function
Thanks a lot. It seems somewhat clearer to me. I still can't understand a few things: That's the output of tracing the function: 1. Trace: (MY-LENGTH '(LIST WITH FOUR SYMBOLS)) 2. Trace: (MY-LENGTH '(WITH FOUR SYMBOLS)) 3. Trace: (MY-LENGTH '(FOUR SYMBOLS)) 4. Trace: (MY-LENGTH '(SYMBOLS)) 5. Trace:...