PDA

View Full Version : [SOLVED] Very simple LISP question



yanollego
July 10th, 2010, 04:54 PM
(define (abs x)
(if (< x 0)
(-x)
x))
;Value: abs

abs 7
;Value: 7

g
;Value: -2

abs g
;Value: -2Why is it not working properly?
I am using MIT/GNU Scheme.

soltanis
July 10th, 2010, 08:06 PM
This is why I hate Scheme -- hard to find proper documentation on it.

...okay, I found out that I had sigscheme installed but it took me like 5 minutes to figure out how to run it ("sscm"). I really hate scheme now.

Okay, so I think your problem is this:



(-x)


Unlike in an imperative language, that does not mean "negate x". In fact, that line crashes my sigscheme interpreter (man, this interpreter really sucks) with an "ill formatted number" error.

Instead, you should use the "-" function with a single argument:



(- x)


Believe it or not, they do have two different meanings. In my interpreter, that does mean "negate x" (it's supposedly an R5RS interpreter by the way).

yanollego
July 11th, 2010, 02:55 AM
New code, still not working. Puzzled.


g
;Value: -2

(define (abs x)
(cond ((< x 0) (- x))
((= x 0) (0))
((> x 0) (x))))
;Value: abs

abs g
;Value: -2

(- g)
;Value: 2

crush304
July 11th, 2010, 03:17 AM
(abs g)

yanollego
July 11th, 2010, 03:30 AM
crush, in Lisp you do not put numbers into round brackets to evaluate them...
The problem is elsewhere....

nvteighen
July 11th, 2010, 09:14 AM
Everytime you want to apply a function to some arguments you use the following syntax:



(function arg0 arg1 ...)


So, use:


(abs -2)


Without parentheses, Scheme will just output the values... or actually, only the last value: in your case, the number itself without further evaluation.

I recommend you to look at the SICP videos (http://groups.csail.mit.edu/mac/classes/6.001/abelson-sussman-lectures/)