Notes from Simply Scheme

Getting setup

Notes from Brian Harvey’s Simply Scheme: Introducing Computer Science

I used Racket 6.10.

Downloaded and saved the files under $HOME/btbytes/code/ss. Subsequently, I loaded the files requried for invidual exercise by using the load command like this – (load "btbytes/code/ss/simply.scm"). Note: given path is relative to the home directory.

Alternate: Install the program files by installing the simply-scheme.plt by typing (require (planet dyoo/simply-scheme:2:2)) in the racket shell.

Test if the installation is successful by typing

(word 'comp 'uter)
computer

Chapter 1

The procedure word is defined in the simply-scheme.plt package.

Typing (exit) in DrRacket will kill the evaluation thread. Get a fresh one by clicking on the Run |> button on the toolbar.

The procedures that scheme when you fire it up initially are the “primitive” procedures. You can define new procedures - “compound procedures”.

Acronym program

Here is a program that finds the acronym for a title:

(define (acronym phrase)
  (accumulate word (every first phrase)))

Use the defined procedure:

(acronym '(too long didnot read))

Redefine acronym to make an acronym only using “real words”. That is, ignore words like a, an, in, the, of, for etc.,

(define (acronym phrase)
    (accumulate word (every first (keep real-word? phrase))))

we define the predicate real-word? like this:

(define (real-word? wd)
  (not (member? wd '(a the an in of an for to with))))

The predicate real-word? returns a #t for true and #f for false.

(real-word? 'of)
#f

(real-word 'association)
#t

Pig Latin

The program converts given word in to pig latin. The rules are:

  1. If the word starts with a vowel, then add ay to the end of the word. if not apply rule 2.
  2. If the word starts with a consonant, move it to the end of the word (“rotate”), and apply rule 1.

So, this is a recursive procedure.

(define (pigl wd)
    (if (member? (first wd) 'aeiou)
        (word wd 'ay)
        (pigl (word (butfirst wd) (first wd)))))
> (pigl 'pig)
'igpay  

Ice cream choices

Given:

  1. 3 sizes – small, medium, large
  2. 4 flavours – vanilla, ultra chocolate, rum raisin, ginger
  3. 2 serving containers – cone, cup

write a program that can enumerate all the choices.

Notice that (cdr '(foo bar baz) is same as (butfirst '(foo bar baz)).

Chapter 2

(load "btbytes/code/ss/functions.ss")

Launch the functions program like this–

> (functions)

Welcome to the FUNCTIONS program.
Function: +
Argument: 3
Argument: 5

The result is: 8

Examples of funtion type

(every first '(the long and winding road))
'(t l a w r)

(define (vowel? letter)
    (member? letter 'aeiou))
(keep vowel? 'constantinople)

Excercises

(word 'now 'here)
'nowhere

(sentence 'now 'here)
'(now here)

(first 'blackbird)
'b
(first '(blackbird))
'blackbird

(+ 3 4)
7

(every butfirst '(thank you girl))
'(hank ou irl)

(member? 'e 'aardvark)
#f

(member? 'the 'they)
#t

(keep vowel? '(i will))