Jump to content

Bad Function error from command line


Recommended Posts

Posted

I've been trying to learn more on the syntax of LISP and as I study it I find it daunting...lots of things to remember and those damn parenthesis are a real pain. But I find that when I'm puzzled with a chunk of code I go to the command line and type it out and watch the results printed in the window. This morning I tried this in an effort to understand the car and cadr commands, but the results are throwing me. From the command window I type:

 

Command: (car (1 2 3))

Error: bad function: 1

 

Okay, at least I get the 1 I'm looking for but I don't get the Error: bad function: part of it. So then I type:

 

Command: (cadr (3 2 1))

Error: bad function: 3

 

This I don't get. I should get back 2 but instead I get 3 and again the bad function error I don't understand.

Posted

your getting back the error because the interpreter is trying to evaluate the inner most statement (your list) and its finding that the number one or three isnt a function. Your intentions are right on you just need to give the interpreter one more piece of info; the quote symbol. Which tells the interpreter not to evaluate what comes next (which is your list in this case of course).

 

So:

(car '(1 2 3))

=> 1

(cadr '(1 2 3))

=> 2

Posted

I shall try to explain this to the best of my ability, and make things as clear as possible, but if you are lost on anything at all, let me know :)

 

Firstly, functions such as car, cadr, caddr, cdadr etc deal with lists and are list manipulators if you like. There is a great explanation on these functions here: http://ronleigh.info/autolisp/afude09.htm#car

 

The interpreter (what you run your program through), does not recognise the following as a list:

 

(a b c)

But rather will try to evaluate the first symbol to the right of the open parenthesis (a), as if it were a function, with b and c being the arguments.

 

And so, just in the way that when you use (car ...) the interpreter evaluates car as it is the first expression after the right parenthesis, and anything after it, the interpreter will treat as arguments of the function car. (until another open parenthesis is reached).

 

As stated, functions like car, cadr, caddr take arguments in the form of lists so you need to provide them with a list.

 

Either:

 

(car '(1 2 3))

Returns:  1

Or

 

(car (list 1 2 3))

Returns:  1

The use of the apostrophe tells the interpreter not to evaluate the next expression, but to take it as an argument. Or, as in the second example, the list function, will take a group of symbols (in this case 1 2 3) and return a list of the symbols, in this case (1 2 3).

 

Hope this makes more sense now. :)

 

If you need anything explained further, just shout :D

 

Cheers

 

Lee

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...