(defun isDigit(x)
(if(eql x 0) T)
(if(eql x 1) T)
(if(eql x 2) T)
(if(eql x 3) T)
(if(eql x 4) T)
(if(eql x 5) T)
(if(eql x 6) T)
(if(eql x 7) T)
(if(eql x 8) T)
(if(eql x 9) T)
(nil)))
🔒 Locked
LISP else
Advertisement
Original Post
I'm quite new to LISP and am trying to write an auxiliary isDigit function tell if the atom passed to it is a digit. The last line there flags an error saying "An object cannot start with #/". Is that not the way you would do an "else"? [Edited by - CyberSlag5k on April 10, 2005 4:12:10 PM]
documentation for if
Your code is totally wrong. Somewhat similar code in c would be (translating errors too)
What editor are you using?
(defun is-digit(x)
(and (<= x 9) (>= x 0)))
is way shorter, but only works if x is a number
Your code is totally wrong. Somewhat similar code in c would be (translating errors too)
bool result;if(x==0)result=true;if(x==1)result=true;if(x==2)result=true;//.if(x==9)result=true;result=false();return result;What editor are you using?
(defun is-digit(x)
(and (<= x 9) (>= x 0)))
is way shorter, but only works if x is a number
Quote:
Original post by Trap
documentation for if
Your code is totally wrong. Somewhat similar code in c would be (translating errors too)
*** Source Snippet Removed ***
What editor are you using?
(defun is-digit(x)
(and (<= x 9) (>= x 0)))
is way shorter, but only works if x is a number
Hmm...I thought the "else" of a LISP if-statement followed the "then". So I interpretted what i wrote as (in C):
if(x == 0) return true; else if((x == 1) return true; else... if(x == 9) return true; else return false;This seems to support that, as does the link you provided, but perhaps I am interpretting both wrong. How should I then do it, might I ask? Unfortunately, x can be anything, it's not necessarily a number.
And I'm just using some version of CLISP I found on source forge...
Thanks for your response!
Quote:
Original post by Anonymous Poster
LISP's if construct is as follows.
(if condition
then
else)
else can be omitted.
This code: (if(eql x 0) T), doesn't have an else clause. It's getting run, and then it drops through to the next thing no matter what. What editor are you using? Emacs (and I'm sure others) can indent LISP for you so that nesting errors become glaringly obvious.
Hope I was helpful.
Ahh, so what I have is this:
(defun isDigit(x) (if(eql x 0) T) (if(eql x 1) T)What I want should be this:
(defun isDigit(x) (if(eql x 0) T (if(eql x 1) T.... nil)))Right?
Ah, I was reading "editor" as "implimentation". Silly me. I'm just using notepad. I'll look into emacs.
Thanks guys!
Quote:
Original post by Anonymous Poster
Yes, but actually you might be better off with a cond here anyway. Looks much better than lots of chained ifs.
(cond ((= x 0) t)
((= x 1) t)
((= x 2) t)
...
(t nil))
cond, eh? Just looked it up in the doc. Looks cool, though I don't understand that final (t nil). The doc has this:
Quote:
Instead of nesting if statements as before, the set of conditions (if A B (if C D E)) can be expressed using cond. It would look like this:
(cond (A B)
(C D)
(t E))
I don't really get that. And don't I want to be using eql instead of =? Or is = also a comparison?
Thanks!
Quote:
Original post by CyberSlag5k
I'm quite new to LISP and am trying to write an auxiliary isDigit function tell
if the atom passed to it is a digit.
The last line there flags an error saying "An object cannot start with #/". Is that not the way you would do an "else"?
The syntax of if is:
(if <test> <true-expr> <false-expr>)The correct way to write what you want is
(defun isDigit(x) (if(eql x 0) T (if(eql x 1) T (if(eql x 2) T (if(eql x 3) T (if(eql x 4) T (if(eql x 5) T (if(eql x 6) T (if(eql x 7) T (if(eql x 8) T (if(eql x 9) T nil)))))))))))That's just a little bit evil, obviously. Here are some less evil solutions:
(defun isDigit (x) (case x ((0 1 2 3 4 5 6 7 8 9) t) (otherwise nil)))(defun isDigit (x) (and (numberp x) (<= x 9) (>= x 0)))(defun isDigit (x) (if (member x '(0 1 2 3 4 5 6 7 8 9)) t))If the number is really a character, you can and should use digit-char-p.
Quote:
Original post by Anonymous Poster
The (t nil) part is pretty much what default: would do in a C switch statement.
That's what I thought, but the example I listed was throwing me. Now I think I see what is going on:
(if(A B) (if C D E))
In LISP says:
if A then Belse if C then D else ESo the default is indeed E. I'm on to you, LISP!
Thanks, AP.
Quote:
(defun isDigit (x)
(case x ((0 1 2 3 4 5 6 7 8 9) t)
(otherwise nil)))
Niiiice. I didn't think I had a case statement available to me. Thanks, Nathan!
Quote:
Original post by CyberSlag5k
cond, eh? Just looked it up in the doc. Looks cool, though I don't understand that final (t nil). The doc has this:
(cond (test action...) (test action...) (test action...) ... )There's nothing magical about the (t nil). cond simply takes each argument in turn, evaluates the test, and then evaluates the actions if the test returned true. t is always true, so if the cond gets to that last argument, the test always succeeds and nil is returned.
Quote:
Quote:
Instead of nesting if statements as before, the set of conditions (if A B (if C D E)) can be expressed using cond. It would look like this:
(cond (A B)
(C D)
(t E))
I don't really get that. And don't I want to be using eql instead of =? Or is = also a comparison?
= is only for numbers. = might signal an error if the argument isn't actually a number. eql won't ever signal an error.
Try Lisp in a Box which includes Emacs, SLIME and a module for CLISP. I only use very few features of it, but they make using Lisp so much more comfortable. Hyperspec-Lookup, parens highlighting and autoindent.
Practical Common Lisp a good introduction to lisp.
Practical Common Lisp a good introduction to lisp.
Got it. Thanks, Nathan.
I downloaded a version of Emacs (with a very non-english installer, I might add) off sourceforge. I'm loving the paranthesis matching feature. Great suggestion! I'll give this whole big package of yours a shot in a little bit, as it would be nice to have a lisp interpreter built into emacs.
Yup, that's one of the two references I'm working from.
Thanks trap!
Here's a quick error, too:
The top line gives "comma is illegal outside of backquote". Is it because each of those is to be a list? (defun myPush((input), (stack), (output)) gives the same error.
Quote:
Try Lisp in a Box which includes Emacs, SLIME and a module for CLISP. I only use very few features of it, but they make using Lisp so much more comfortable. Hyperspec-Lookup, parens highlighting and autoindent.
I downloaded a version of Emacs (with a very non-english installer, I might add) off sourceforge. I'm loving the paranthesis matching feature. Great suggestion! I'll give this whole big package of yours a shot in a little bit, as it would be nice to have a lisp interpreter built into emacs.
Quote:
Practical Common Lisp a good introduction to lisp.
Yup, that's one of the two references I'm working from.
Thanks trap!
Here's a quick error, too:
(defun myPush(input, stack, output) (if(or (nil input) (nil isDigit((car input))) interpret(input, 'illegal, 'illegal)) interpret((cdr input), append((car input) stack), output))The top line gives "comma is illegal outside of backquote". Is it because each of those is to be a list? (defun myPush((input), (stack), (output)) gives the same error.
You don't need the comma in this place.
Read an introduction on Lisp before mindlessly writing stuff. Some simple things:
-everything in () is a list
-the first element in every list is the name of the function to be called, the rest are the arguments
-elements in a list are seperated by whitespace
Emacs with SLIME is way better than Emacs alone (about as much as Emacs is better than Notepad). It offers "Lisp in Emacs" + autocompletion + onlinehelp + loading files into Lisp via a simple keychord + a lot more.
Read an introduction on Lisp before mindlessly writing stuff. Some simple things:
-everything in () is a list
-the first element in every list is the name of the function to be called, the rest are the arguments
-elements in a list are seperated by whitespace
Emacs with SLIME is way better than Emacs alone (about as much as Emacs is better than Notepad). It offers "Lisp in Emacs" + autocompletion + onlinehelp + loading files into Lisp via a simple keychord + a lot more.
Quote:
Original post by Trap
You don't need the comma in this place.
Ah, yes, like defining any list, you do it without the commas.
Quote:
Read an introduction on Lisp before mindlessly writing stuff. Some simple things:
-everything in () is a list
-the first element in every list is the name of the function to be called, the rest are the arguments
-elements in a list are seperated by whitespace
I've read 2 introductions to LISP and my instructor has gone over it in a class I'm taking. I wouldn't say I'm "mindlessly writing stuff", just because I forgot the third point on your list there. We've been working in ML as of late and this is the first LISP program I've written in a month, and is perhaps the 4th LISP program I've written ever. You might say I am new to LISP or that I am unfamiliar with the syntax, but mindless isn't fair.
Anyway, thank you for pointing that out.
Quote:
Emacs with SLIME is way better than Emacs alone (about as much as Emacs is better than Notepad). It offers "Lisp in Emacs" + autocompletion + onlinehelp + loading files into Lisp via a simple keychord + a lot more.
I'll check it out now.
Quote:
Original post by Trap
some more errors in your code:
func(args) is c-style, (func args) is lisp
Everything after a ( has to be something callable (if it is evaluated, some macros like "let" don't evaluate all arguments). (or (nil... doesn't make sense.
Ah, thanks again. I do that quite a bit.
Quote:
(or (nil... doesn't make sense.
Those are like that because I wanted I thought I needed to divide up the expressions. nil isDigit is meant to be one expression, while nil input is another. I suppose the compiler is smart enough to separate them on their own, then?
[Edited by - CyberSlag5k on April 10, 2005 7:35:34 PM]
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement