capably_chummy
capably_chummy

Reputation: 17

Scheme contract violation in DrRacket

I am following Brian Harvey’s SICP lectures at archive.org I am using the DrRacket v7.4 IDE to write my Scheme code. At 06:39 Professor Brian Harvey shows how to select the first character of a string. When I follow this method exactly I do not get the expected result, as shown below. Why might that be?

My code looks like this:

# lang scheme

(first 'hello)

Expected result:

h

Error message:

first: contract violation
expected: (and/c list? (not/c empty?))
given: hello

Upvotes: 0

Views: 399

Answers (1)

Sylwester
Sylwester

Reputation: 48745

While it is the same Science course as SICP does it is not vanilla SICP as the magicians did it. Brian Harvey has clearly used some procedures he is using in another book ha has on Scheme, called Simply Scheme (free pdf online | Amazon).

In it he uses the terms words and sentences and makes procedure that is kind of object oriented in the way that (first var) will return the first letter if var is either a string or symbol or the first "word" (element) if it is a list. In fear of violating copyright it's defined on page 531 in the PDF version.

Anyway it is not standard so in order for it to work he either has loaded the procedures or made an ini file that defines them at startup. In Racket you can do the same by creating your own language or requiring a library. Sure enough someone has taken the time to create it as a language in Racket so you can do this:

#lang simply-scheme
(se (butlast (bf "this"))
    "world")

The first time you run this it will ask in the bottom to install Simply Scheme. Just press Install and the run once more once it is finished.

While I have nothing against Brian Harvey I don't believe this is better than the original SICP (videos | html book | webpage with resources) that also has their own language in DrRacket.

Note that none of these will be compatible with modern Scheme. Some of the procedures has altered contract, changed name or been totally replaced. Compare it with trying to learn English by reading Shakespeare in the way that the grammar won't help you.

Other resources like this as How to design programs (html book), Rackets own and recommended book and, my favorite, Realm of Racket (web page).

Upvotes: 2

Related Questions