【问题标题】:How to convert the explicit recursive functions into Higher-Order/Abstract Functions in Scheme如何在 Scheme 中将显式递归函数转换为高阶/抽象函数
【发布时间】:2016-02-22 12:25:04
【问题描述】:

我想知道如何使用 Scheme/Racket 中的 foldr、map、filter 等将下面的显式递归函数转换为高阶/抽象函数。

(define (insertNoDups f element lst)
  (cond[(empty? lst) (cons element lst)]
       [(f element (first lst)) lst]
       [else (cons (first lst) 
                   (insertNoDups f element (rest lst)))]))

(define (remove-dups f lst)
  (cond[(empty? lst) empty]
       [else (insertNoDups  f 
                            (first lst) 
                            (remove-dups f (rest lst)))]))

【问题讨论】:

    标签: recursion scheme racket fold higher-order-functions


    【解决方案1】:

    insertNoDups(将其重命名为 insert-no-dups):使用 member-procedure 谓词(更多信息 -> http://www.gnu.org/software/mit-scheme/documentation/mit-scheme-ref/Searching-Lists.html)如果找不到,则将 element 放在前面。

    remove-dups:查看 Óscar López 的答案 -> How to remove all the duplicates in a list using scheme (only abstract list functions allowed) 并使用 member-procedure 谓词 而不是 member?

    【讨论】:

    • 感谢您的回复。但我想要的是将上面的两个函数组合成一个使用抽象函数的函数。函数中的“f”也指使用两个变量的函数,如 (lambda (x y)...)。你能帮我弄清楚这个吗?谢谢。
    • 并不难弄清楚,可能值得研究您想要应用的概念。因为目前您似乎不知道他们在做什么。
    • 我真的不知道这个......我已经尝试了一整天将这些函数转换为抽象函数,但没有任何结果......请帮助......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-22
    • 1970-01-01
    • 1970-01-01
    • 2016-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多