Scheme contract violation in DrRacket

青春壹個敷衍的年華 提交于 2019-12-02 17:12:45

问题


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

回答1:


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).



来源:https://stackoverflow.com/questions/58308445/scheme-contract-violation-in-drracket

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!