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
word is defined in the
(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”.
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))
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))))
real-word? returns a
#t for true and
#f for false.
(real-word? 'of) #f (real-word 'association) #t
The program converts given word in to pig latin. The rules are:
- If the word starts with a vowel, then add
ayto the end of the word. if not apply rule 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
- 3 sizes – small, medium, large
- 4 flavours – vanilla, ultra chocolate, rum raisin, ginger
- 2 serving containers – cone, cup
write a program that can enumerate all the choices.
(cdr '(foo bar baz) is same as
(butfirst '(foo bar baz)).
functions program like this–
> (functions) Welcome to the FUNCTIONS program. Function: + Argument: 3 Argument: 5 The result is: 8
- type – a kind of data (numbers, words, sentences, booleans, fuctions)
- domain – the things the function accepts as an argument
- range – the things that the function returns
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)
(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))