【问题标题】:Exiting from a recursive call in a functional language退出函数式语言的递归调用
【发布时间】: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


    【解决方案1】:

    您需要让函数句柄 L 为空,无论该操作是什么。

    (define (first-item L)
      (if (empty? L)
        #| exit |#
        #| recursive call |#))
        
    

    【讨论】:

      猜你喜欢
      • 2021-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-30
      • 2015-01-28
      • 2011-09-04
      • 2017-06-28
      • 2020-04-28
      相关资源
      最近更新 更多