Chinaunix

标题: 请教这是为什么?DrScheme中的string-set! [打印本页]

作者: pass12163com    时间: 2009-07-11 21:44
标题: 请教这是为什么?DrScheme中的string-set!
(define x "hello")
x
==>"hello"
(string-set! x 0 #\p)
==>string-set!: expects type <mutable string> as 1st argument, given: "hello"; other arguments were: 0 #\p ;;;;;;;;;;;;结果错误???

(define x (string #\h#\e#\l#\l#\o))
x
==>"hello"
(string-set! x 0 #\p)
x
==>"pello"  ;;;;;;;;结果正确
作者: win_hate    时间: 2009-07-11 22:09
3.3 strings

......

A string can be mutable or immutable. When an immutable string is provided to a procedure like string-set!, the exn:fail:contract exception is raised. String constants generated by the default reader (see Reading Strings) are immutable.

12.6.6 Reading Strings

When the reader encounters ", it begins parsing characters to form a string. The string continues until it is terminated by another "

作者: pass12163com    时间: 2009-07-11 23:10
多谢。
查了一下r5rs,好像没有上面的表述。是什么书里的?
作者: win_hate    时间: 2009-07-12 00:06
DrScheme 自带的 help 文档。网上也有:

http://docs.plt-scheme.org/reference/strings.html

只读是 drscheme 的个性吧,我用 guile 试,"hello" 也是可改写的。
作者: chenzengjie    时间: 2009-07-13 10:15
原帖由 pass12163com 于 2009-7-11 23:10 发表
多谢。
查了一下r5rs,好像没有上面的表述。是什么书里的?


可变串应该不是标准scheme类型。
<scheme程序设计语言>也没有提到。
sicp里除了error用到串, 很少提及串这东西。 和Unix里到处都是字符串的情况真是巨大的差别。
把以前的东西暂时都忘了,才能学到scheme里遍地的“新”东西。




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