examine

Discussion of Common Lisp
Post Reply
burton
Posts: 10
Joined: Sat Aug 28, 2010 6:05 pm

examine

Post by burton » Sun Sep 05, 2010 7:16 am

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.

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: examine

Post by ramarren » Sun Sep 05, 2010 7:56 am

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.

burton
Posts: 10
Joined: Sat Aug 28, 2010 6:05 pm

Re: examine

Post by burton » Sun Sep 05, 2010 10:59 am

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

?

audwinc
Posts: 12
Joined: Thu Sep 02, 2010 11:46 am

Re: examine

Post by audwinc » Sun Sep 05, 2010 5:25 pm

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.

burton
Posts: 10
Joined: Sat Aug 28, 2010 6:05 pm

Re: examine

Post by burton » Sun Sep 05, 2010 5:35 pm

thanx, i managed to solve it.

Post Reply