Finding Depth of an Expression

You have problems, and we're glad to hear them. Explain the problem, what you have tried, and where you got stuck.
Feel free to share a little info on yourself and the course.
Forum rules
Please respect your teacher's guidelines. Homework is a learning tool. If we just post answers, we aren't actually helping. When you post questions, be sure to show what you have tried or what you don't understand.

Finding Depth of an Expression

Postby crimsondoll » Mon Nov 12, 2012 9:16 am

Hi, I'm incredibly new to Lisp and I've got to write a program that calculates the depth of an expression. Here's what I've got so far:

Code: Select all
 (DEFUN depth (L)
(IF (NIL L) 0)
(IF (NIL (SECOND L)) 0)
(IF (ATOM (SECOND L)) (1+depth(CDR L)))
)


What it's supposed to do is return 0 if the list L is empty or only has one atom (the two base cases) and if not, return 1 plus the depth of the CDR of the list. Whenever I try to test the code, I get an illegal argument error. I tried to test the function with

Code: Select all
(depth (1 2 4))


and get this:
Code: Select all
Error: Illegal argument in functor position: 1 in (1 2 4).


Any help would be much appreciated.
crimsondoll
 
Posts: 1
Joined: Mon Nov 12, 2012 9:09 am

Re: Finding Depth of an Expression

Postby Paul » Mon Nov 12, 2012 2:50 pm

crimsondoll wrote:Hi, I'm incredibly new to Lisp and I've got to write a program that calculates the depth of an expression. Here's what I've got so far:

Code: Select all
 (DEFUN depth (L)
(IF (NIL L) 0)
(IF (NIL (SECOND L)) 0)
(IF (ATOM (SECOND L)) (1+depth(CDR L)))
)


What it's supposed to do is return 0 if the list L is empty or only has one atom (the two base cases) and if not, return 1 plus the depth of the CDR of the list. Whenever I try to test the code, I get an illegal argument error. I tried to test the function with

Code: Select all
(depth (1 2 4))


and get this:
Code: Select all
Error: Illegal argument in functor position: 1 in (1 2 4).


Any help would be much appreciated.


Your first problem is the way you call it: (depth (1 2 4)) is attempting to call DEPTH on the result of calling 1 with arguments 2 and 4 -- 1 is not a function, so you get an error. You need to say (depth (quote (1 2 4))) to stop it trying to evaluate (1 2 4) -- or equivalently, (depth '(1 2 4))

The next problem is the structure of your function: a bunch of IFs one after another, but it doesn't return the value they generate -- if L is NIL, the first IF will evaluate to 0, but the code will go on to evaluate the next IF, and then the third, which will evaluate as NIL since the test is false if L is NIL; the function will return NIL, not 0. If the test in the last IF is not NIL, it'll attempt to call 1+ on NIL (since eventually the recursive calls bottom out in the previous case) -- or at least it would if you fixed the syntax there -- but (1+ nil) is an error...
Paul
 
Posts: 106
Joined: Tue Jun 02, 2009 6:00 am

Re: Finding Depth of an Expression

Postby Goheeca » Mon Nov 12, 2012 3:28 pm

Look at cond and instead of nil you mean null.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version).
User avatar
Goheeca
 
Posts: 213
Joined: Thu May 10, 2012 12:54 pm

Re: Finding Depth of an Expression

Postby sylwester » Tue Nov 13, 2012 4:53 pm

It took a while to see that you're making your own length function.
Interestingly, your function has sequential if statements,. Even if NIL was a function (which clearly you mean NULL) The first IF would evaluate to 0 and then it will continue to the second if. You really want to use (if (null L) <then-expression> <else-expression>). The recursion (1+ (depth (cdr L))) lacked parenthesis around depth.

If you have a list '() it should return 0, but if it's '(q) (second L) would return NIL end result is 0 even though the length is clearly 1. I don't think you need more than one base case..
I'm the author of two useless languages that uses BF as target machine.
Currently I'm planning a Scheme compiler :p
sylwester
 
Posts: 83
Joined: Mon Jul 11, 2011 2:53 pm


Return to Homework

Who is online

Users browsing this forum: No registered users and 0 guests

cron