Really, really basic question (negative numbers)

Discussion of Scheme and Racket

Really, really basic question (negative numbers)

Postby keval » Tue Apr 28, 2009 10:11 pm

Greetings.
For what I'm sure is a really basic reason, I can't get negative numbers to work. I keep getting error messages about an undefined identifier. A simple example (finding the absolute value of an integer) that's copied directly out of SICP:

(define (ab x)
(cond ((< x 0) (-x))
(else x)))
> (ab 8)
8
> (ab -8)
. . reference to undefined identifier: -x

Can someone fill me in?
Thanks,
Kevin
keval
 
Posts: 6
Joined: Tue Apr 28, 2009 10:06 pm

Re: Really, really basic question (negative numbers)

Postby gugamilare » Wed Apr 29, 2009 7:34 pm

You need to put a space between - and x, or else the interpreter / compiler will think that -x is a symbol.
gugamilare
 
Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil

Re: Really, really basic question (negative numbers)

Postby keval » Wed Apr 29, 2009 9:01 pm

Ah. I told you it was a basic question.
Many thanks. Works like a charm.
Kevin
keval
 
Posts: 6
Joined: Tue Apr 28, 2009 10:06 pm


Return to Scheme

Who is online

Users browsing this forum: No registered users and 1 guest