Page 1 of 1

examine

Posted: Sun Sep 05, 2010 7:16 am
by burton
hi all,
i wanna examine the atoms in a nested list, if the list contains numbers only (return t), if there is a char or other symbol (return nil)


(defun examine (lst)
(cond ((null list) 0)
((atom list) 1)
(if numberp lst (examine (car lst))
???)))
)
)
im stuck here.

Re: examine

Posted: Sun Sep 05, 2010 7:56 am
by ramarren
Your code sample makes no sense. Try to explain logically in words what you want to do on algorithmic level, and then implement it in code. Also, this obviously looks like homework, so you should state the exact limitations specified, because for example in this case EVERY might be useful.

Re: examine

Posted: Sun Sep 05, 2010 10:59 am
by burton
EVERY would be useful, however i need a recursive function

defun examine (list)
cond list empty return nil
if list element is a number go to next element, call examine again,
else return nil

?

Re: examine

Posted: Sun Sep 05, 2010 5:25 pm
by audwinc
I think I know where you are coming from with this question. Here is another way to phrase it:

You have a tree. If any of the leaf nodes is not a number, return nil. Otherwise, return t. Use recursion to navigate to the leaves of the tree to test for numberness.

Regarding the code you posted, you are starting on the right track. You just have a few loose ends. Your null list should probably return nil (no numbers). You should test the atom function out on symbols and numbers; you will find that it returns t for both. numberp might be more along the lines of what you are looking for.

Now for your list examination... I see what you are trying to do, and you should research out what car and cdr do for you. It makes no sense to do numberp on a list. You might want to do a listp on some element of your list so that you can further examine that nested list.

Look around for "breadth-first" and "depth-first" search. Either of those approaches will help you towards your goal.

Re: examine

Posted: Sun Sep 05, 2010 5:35 pm
by burton
thanx, i managed to solve it.