multiple expressions in function body?

799 views
Skip to first unread message

bpie...@gmail.com

unread,
Jun 6, 2014, 9:39:27 AM6/6/14
to bootstra...@googlegroups.com
In the Racket documentation, I see that a function can contain multiple expressions, such as:

(define (bake flavor)
  (printf "pre-heating oven...\n")
  (string-append flavor " pie")) http://docs.racket-lang.org/guide/syntax-overview.html When I try this in WeScheme, I get the following error:

define: expected only one expression for the function body, but found 1 extra part

at: line 3, column 0, in <definitions>



Kathi Fisler

unread,
Jun 6, 2014, 11:14:34 AM6/6/14
to bootstra...@googlegroups.com
In Bootstrap, we use a restricted version of Racket that is tailored to what beginning students know.  This lets us do things like provide error messages that are better tailored to novice programmers.

Multiple expressions are not allowed in a function body in the Bootstrap version of the language.  We do this because a single expression in the body is consistent with functions in math: functions consume inputs and produce outputs, but don't "change the world around them" (including the screen) in the process.  

This does rule out using printf to show how a program is executing (as you are trying to do here).  For those coming to Bootstrap from prior programming experience, this might seem strange at first.  Again, it comes from thinking about functions from the perspective of algebra rather than programming.

Kathi


--
You received this message because you are subscribed to the Google Groups "Bootstrap-Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bootstrap-disc...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

bpie...@gmail.com

unread,
Jun 6, 2014, 12:04:30 PM6/6/14
to bootstra...@googlegroups.com
Thanks for the explanation.  The question arose because I have a student who wants to use variables in update-player to:

1) define a local variable based on a key that is pressed
2) use that variable in the output of the function

Any other ideas for how to do something similar in Bootstrap?

Thanks!

Shriram Krishnamurthi

unread,
Jun 6, 2014, 12:24:48 PM6/6/14
to bootstra...@googlegroups.com
​You certainly can. Here is an example:

(define (f x)
  (local ([define x-squared (* x x)])
    (sqrt (+ x-squared x-squared))))

Note that local uses the same syntax internally as the top-level does, so you can "import" top-level definitions inside a function.

Here's an example with two local variables:

(define (f x y)
  (local ([define x-squared (* x x)]
          [define y-squared (* y y)])
    (sqrt (+ x-squared y-squared))))

where (f 3 4) produces, as you would expect, 5.

Is that the sort of thing your student is trying to do? If so, do these examples help?

Shriram

bpie...@gmail.com

unread,
Jun 6, 2014, 12:36:21 PM6/6/14
to bootstra...@googlegroups.com
That will ABSOLUTELY help, and thank you for the examples!




--
Reply all
Reply to author
Forward
0 new messages