gpt4 book ai didi

string - Lisp Scheme 使用 (string-set!) 遇到错误

转载 作者:太空宇宙 更新时间:2023-11-03 18:52:49 25 4
gpt4 key购买 nike

我是 (Lisp) 方案编程的新手,一直在学习教程。在我使用 string-set 之前一切都很顺利!

字符串设置!用于替换索引处的字符。下面是我所做的示例:

Welcome to Racket v5.1.3.
> (define greeting "Hello; Hello!")
> (string-set! greeting 1 #\a)
string-set!: expects type <mutable string> as 1st argument, given: "Hello; Hello!"; other arguments were: 1 #\a

=== context ===
/usr/share/racket/collects/racket/private/misc.rkt:85:7

> greeting
"Hello; Hello!"
>

(string-set! greeting 1 #\a)执行后,它应该根据教程将第一个 'e' 替换为 'a'。输出应该是:

> greeting
"Hallo; Hello!"

但是我收到了错误 string-set!: expects type <mutable string> as 1st argument .我做错了吗?

最佳答案

根据 Racket documentation :

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, and they are interned in read-syntax mode.

您可以使用 string-copy 复制一个字符串,这总是返回一个可变字符串。

(define greeting (string-copy "Hello; Hello!"))

关于string - Lisp Scheme 使用 (string-set!) 遇到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17769396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com