Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to design programs problem on chapter 1 (Racket)

525 views
Skip to first unread message

Roelof Wobben

unread,
Jan 29, 2012, 7:05:08 AM1/29/12
to
Hello

One of the first exercises I have to make a Faharenheit -> Celcius
converter.

The formula is Celcius = (Fahrenheit - 32) / 1.8

So I did this :

def (Fahrenheit to Celcius)
/( - (F 32)+1) /( 8 10)

But I see this message :

<unsaved editor>:2:3: /: expected a function call, but there is no open
parenthesis before this function in: /


Can anyone help me figure out where my thinking error is ?

Roelof

Roelof Wobben

unread,
Jan 29, 2012, 11:50:18 AM1/29/12
to
Hello,

I did change it to this now :

(define (Fahrenheit-Celcius x)
(/(-(x 32))1.8))

But now Im getting this error :

<unsaved editor>:2:6: function call: expected a function after the
open parenthesis, but found a variable in: x


So I find this way of calculating things confusing.

Roelof



Op 29-1-2012 13:05, Roelof Wobben schreef:

Hans Aberg

unread,
Jan 29, 2012, 11:56:50 AM1/29/12
to
On 2012/01/29 17:50, Roelof Wobben wrote:
> Hello,
>
> I did change it to this now :
>
> (define (Fahrenheit-Celcius x)
> (/(-(x 32))1.8))
>
> But now Im getting this error :

Perhaps this might help:
https://en.wikipedia.org/wiki/S-expression

Hans


Roelof Wobben

unread,
Jan 29, 2012, 12:54:55 PM1/29/12
to
Hello Hans,

Thanks for the tip but it was not helpfull.
I still don't see what I did wrong.

As far as I understand this I have to read this as follows :

Suppose x=2

(/(-(x 32) 1.8))

(/(-(2-32) 1.8)
(/(-30 1.8)
30/1.8
-16.666667

Roelof




Op 29-1-2012 17:56, Hans Aberg schreef:

Hans Aberg

unread,
Jan 29, 2012, 1:25:37 PM1/29/12
to
On 2012/01/29 18:54, Roelof Wobben wrote:

> Thanks for the tip but it was not helpfull.
> I still don't see what I did wrong.
>
> As far as I understand this I have to read this as follows :
>
> Suppose x=2
>
> (/(-(x 32) 1.8))
>
> (/(-(2-32) 1.8)
> (/(-30 1.8)
> 30/1.8
> -16.666667

A function call is written with its name followed by its arguments, with
an enclosing "(...)", that can be thought of as causing the call to
happen (passing it to the evaluator). Then just nest it.

So
x - 32 -> (- x 32)
y/1.8 -> (/ y 1.8)
In the last, substitute y with expression above to get
(/ (- x 32) 1.8)

To make this into a function, mapping x to the expression f, use the lambda
(lambda (x) f)
Substitute f with the expression above:
(lambda (x) (/ (- x 32) 1.8))

Give it a name (in Scheme):
(define F-to-C (lambda (x) f))
or
(define F-to-C (lambda (x) (/ (- x 32) 1.8)))

Evaluate
(F-to-C -40)
--> -40

Actually, there is a shortcut, or "syntactic sugar", for
(define <name> (lambda (x) f))
namely
(define (<name> x) f)

So the above definition can equivalently be written
(define (F-to-C x) (/ (- x 32) 1.8))

See for example the book, which nowadays in online:
http://mitpress.mit.edu/sicp/
http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-4.html

Hans

Jussi Piitulainen

unread,
Jan 29, 2012, 1:30:47 PM1/29/12
to
Roelof Wobben writes:

> I still don't see what I did wrong.
>
> As far as I understand this I have to read this as follows :
>
> Suppose x=2
>
> (/(-(x 32) 1.8))

Usually this is spaced (/ (- (x 32) 1.8)).

(/ (- (x 32) 1.8) is a procedure call.
The procedure is the value of /.
The sole argument is the value of (- (x 32) 1.8).

(- (x 32) 1.8) is a procedure call.
The procedure is the value of -.
The second argument is the literal 1.8.
The first argument is the value of (x 32).

(x 32) is a procedure call.
But the value of x is not a procedure.

Racket told a funny story to you. The error was not that x is a
variable. The error was that the value of x is not a procedure.

What you want to write is (- x 32).
It is a procedure call, the procedure is the value of -,
the second argument is the literal 32, and the first
argument is the value of the variable x, which is 2.
Calling - with 2 and 32 does give -30.

And you want to call / with two arguments, 2 and 1.8, to get whatever.
Place the parentheses so: (/ (- x 32) 1.8). Should work.

Roelof Wobben

unread,
Jan 29, 2012, 2:08:51 PM1/29/12
to
Thanks.

it worked now and I know what I did wrong.
So now the next exercise.

Roelof



Op 29-1-2012 19:30, Jussi Piitulainen schreef:

Eli Barzilay

unread,
Jan 29, 2012, 4:51:30 PM1/29/12
to
Jussi Piitulainen <jpii...@ling.helsinki.fi> writes:

> Roelof Wobben writes:
>
>> I still don't see what I did wrong.
>>
>> As far as I understand this I have to read this as follows :
>>
>> Suppose x=2
>> [...]
>
> Racket told a funny story to you. The error was not that x is a
> variable. The error was that the value of x is not a procedure.

Actually, in the beginner language (which is the proper one for this
exercise), Racket told him the exact story. The beginner language is
a first-order language where functions can only be called, nothing
else, so there are no function values in this language. Therefore, in
this language, a "variable" (an input to `Fahrenheit-Celcius') is
something that you *cannot* call. Incidentally, this restriction of
the language is designed exlpicitly for helping students like Roelof,
and for his code, it hit in the exact right place. (Compare that
error with what you get from a straughtforward Scheme implementation.)

--
((lambda (x) (x x)) (lambda (x) (x x))) Eli Barzilay:
http://barzilay.org/ Maze is Life!

Jussi Piitulainen

unread,
Jan 30, 2012, 1:52:38 AM1/30/12
to
Eli Barzilay writes:
> Jussi Piitulainen writes:
>
> > Roelof Wobben writes:
> >
> >> I still don't see what I did wrong.
> >>
> >> As far as I understand this I have to read this as follows :
> >>
> >> Suppose x=2
> >> [...]
> >
> > Racket told a funny story to you. The error was not that x is a
> > variable. The error was that the value of x is not a procedure.
>
> Actually, in the beginner language (which is the proper one for this
> exercise), Racket told him the exact story. The beginner language is
> a first-order language where functions can only be called, nothing
> else, so there are no function values in this language. Therefore, in
> this language, a "variable" (an input to `Fahrenheit-Celcius') is
> something that you *cannot* call. Incidentally, this restriction of
> the language is designed exlpicitly for helping students like Roelof,
> and for his code, it hit in the exact right place. (Compare that
> error with what you get from a straughtforward Scheme implementation.)

Yes, I thought there might be an explanation along these lines, but I
didn't know the details. Thank you.

But as far as I can see, in this case the usual message would be fine:
trying to call 2 but it is not a procedure.

ERROR in final-resumer: non procedure application: 2

Eli Barzilay

unread,
Jan 30, 2012, 9:50:50 AM1/30/12
to
Jussi Piitulainen <jpii...@ling.helsinki.fi> writes:
>
> Yes, I thought there might be an explanation along these lines, but
> I didn't know the details. Thank you.
>
> But as far as I can see, in this case the usual message would be
> fine: trying to call 2 but it is not a procedure.

The original error message in the beginner language:

expected a function after the open parenthesis, but found a variable
in: x

is completely accurate when you considering the limitation of the
language to a first-order one, but more importantly it is accurate
considering the vocabulary of beginners at that level. It is kind of
similar to some "trying to call 2", but the important differences are:

* It's phrased in terms of the syntax (open parenthesis), rather than
semantics (function application) -- since beginners spend more time
fighting with the syntax before they learn the semantics (in all
languages, but more in lisps that get a good number of {}-refugees.)

* It's a syntax error, which means that it points to the exact place
in the code where the offending expression is.
0 new messages