Chinaunix

标题: scheme :reference to undefined identifier [打印本页]

作者: qinguan0619    时间: 2011-07-19 09:19
标题: scheme :reference to undefined identifier
本帖最后由 qinguan0619 于 2011-07-19 09:20 编辑

开始学习SICP,用scheme,下了Racket。按着练习做了几个,遇到问题如下:

  1. > (not 1)
  2. not: expected either true or false; given 1
  3. > (not #f)
  4. true
  5. > (not 'a)
  6. not: expected either true or false; given 'a

  7. > (define name "hhh")
  8. > name
  9. "hhh"
  10. > (string-length name)
  11. 3
  12. > (string-set! name 0 #\g)
  13. reference to undefined identifier: string-set!
  14. >  (define p (cons 4 5))
  15. cons: second argument must be of type <list or cyclic list>, given 4 and 5
复制代码
我是按照http://www.ibm.com/developerworks/cn/linux/l-schm/index1.html的语法。
为什么会出现上面的reference to undefined identifier 以及(not 'a)异常呢?
作者: zhlong8    时间: 2011-07-19 21:43
DrRacket 左下角语言换成 scheme 就行了, Racket 和标准的 scheme 不完全一样。
作者: qinguan0619    时间: 2011-07-20 17:39
本帖最后由 qinguan0619 于 2011-07-20 17:43 编辑
DrRacket 左下角语言换成 scheme 就行了, Racket 和标准的 scheme 不完全一样。
zhlong8 发表于 2011-07-19 21:43

尴尬,我发现我的DrRacket(版本5.1.1)没有scheme语言。。。我选了R5RS,(not 'a)已可以用了,但string-set!还是不行。
所有语言如下所示:

作者: zhlong8    时间: 2011-07-20 23:02
回复 3# qinguan0619


    你可以看下 Racket 的文档看处理 string 的相关函数是什么,scheme 本身就是原始的 lisp 加上几个概念,处理字符串或者 (not 'a) 什么的都是实现细节不需要完全照搬书。
作者: qinguan0619    时间: 2011-07-20 23:54
本帖最后由 qinguan0619 于 2011-07-20 23:56 编辑

回复 4# zhlong8

文档给了个例子:

  1. Examples:

  2. > (define s (string #\A #\p #\p #\l #\e))
  3. > (string-set! s 4 #\y)
  4. > s
  5. "Apply"
复制代码
我试了一下:

  1. > (define p "hhhh")
  2. > (string? p)
  3. #t
  4. > (string-set! p 2 #\y)
  5. . . string-set!: expects type <mutable string> as 1st argument, given: "hhhh"; other arguments were: 2 #\y
  6. > (define q (string #\h #\h #\h #\h))
  7. > q
  8. "hhhh"
  9. > (string-set! q 2 #\y)
  10. > q
  11. "hhyh"
  12. > p
  13. "hhhh"
  14. >

复制代码
根据错误信息,string-set!需要的是一个可变的字符串作为第一个参数.
这个为什么用string来定义字符串就正确了...
(define p "hhhh")与(define q (string #\h #\h #\h #\h))这个定义有什么区别?
作者: zhlong8    时间: 2011-07-21 09:09
回复  zhlong8

文档给了个例子:我试了一下:根据错误信息,string-set!需要的是一个可变的字符串作为第一 ...
qinguan0619 发表于 2011-07-20 23:54



    不是一种数据类型,一种是 string 是不可变的,一种是 mutable string 可变的。不可变的字符串是可以做很多优化的,这里的区分纯粹是实现的原因




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2