【发布时间】:2022-12-09 06:34:00
【问题描述】:
我目前正在学习球拍,并且很难理解如何使用函数式语言进行编程。我试图让函数 first-item 将列表的第一个元素与数字或字符匹配,将该标记添加到结果列表,然后对列表的其余部分进行操作。目前在 (first-item(rest L)) 的最后一次调用中,它发送了一个空列表,然后我的 let 语句失败了,因为它无法在空列表上工作。如何添加退出子句或让我的函数在空列表中结束?
(define(first-item L)
(let ([item (first L)])
(cond
[(regexp-match #rx"[-()+*]" (make-string 1 item)) (first-item (rest L))]
[(regexp-match #px"[0-9]" (make-string 1 item)) (first-item (rest L))]
)
)
)
【问题讨论】:
标签: functional-programming scheme racket