Variables and Procedures

Last updated on 2025-07-05 | Edit this page

Overview

Questions

  • How do you use variables and procedures in Scheme?

Objectives

  • Explore differences between Gambit Scheme and Guile Scheme

Introduction


An alternative implementation of the exercies in variables and procedres section of the Spritely Scheme Primer ported to Gambit Scheme.

SCHEME

(define (chatty-add chatty-name . nums)                                                   
     (string-append chatty-name ", If you add these together you get " (number->string (apply + nums)) "!\n"))

SCHEME

(define (shopkeeper thing-to-buy    
           #!optional (how-many 1)
           (cost 20)
           #!key (shopkeeper "Sammy")
           (store "Plentiful Great Produce"))
           (display
            (string-append "You walk into "
                           store
                           ", grab something from the shelves,\n"
                           "and walk up to the counter.\n\n"
                           shopkeeper
                           " looks at you and says "
                           "'"
                           (number->string how-many)
                           " "
                           thing-to-buy
                           ", eh? That will be "
                           (number->string (* cost how-many))
                           " coins!'\n")))

SCHEME

(shopkeeper "apples")

OUTPUT

You walk into Plentiful Great Produce, grab something from the shelves,
and walk up to the counter.

Sammy looks at you and says '1 apples, eh? That will be 20 coins!'

SCHEME

(shopkeeper "bananas" 10 28)

OUTPUT

You walk into Plentiful Great Produce, grab something from the shelves,
and walk up to the counter.

Sammy looks at you and says '10 bananas, eh? That will be 280 coins!'

SCHEME

(shopkeeper "screws" 3 2
               shopkeeper: "Horace"
               store: "Horace's Hardware")

OUTPUT

You walk into Horace's Hardware, grab something from the shelves,
and walk up to the counter.

Horace looks at you and says '3 screws, eh? That will be 6 coins!'

Need newer version of interpreter for let-values # https://stackoverflow.com/questions/77866133/how-to-use-let-values-in-gambit-scheme

Key Points
  • Scheme implementations may differ in exact functionality offered