gpt4 book ai didi

functional-programming - Scheme中的模式匹配

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

如何接受以下输入?

(list of 0 or more charcters and ends with 3) or   
(list of 1 or more characters 4 and 0 or more characters after 4)

有点像

(match ( list 3)) -> #t
(match ( list 1 2 3)) -> #t
(match (list 1 2 3 4)) -> #t
(match (list 1 2 3 4 5)) -> #t
(match (list 4)) -> #f

编辑:这不是我的作业。我试图从 PAIP 中编写类似 ELIZA 的内容,但我只知道如何编写以单词开头的模式。

最佳答案

您提到了字符,但随后在您的示例中使用了数字。我在这里使用数字,但切换到字符是微不足道的。

(require scheme/match)
(define satisfies
(match-lambda
[(list (? number?) ... 3) #t]
[(list (? number?) ..1 4 (? number?) ...) #t]
[_ #f]))

关于functional-programming - Scheme中的模式匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1053312/

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