如何实现haskell`\\`函数?

发布于 2024-12-05 01:32:56 字数 243 浏览 3 评论 0原文

在 haskell 中, [1,2,3,4,5,6,7] \\ [4,5,6] 将返回 [1,2,3,7]代码>.现在我想使用 clisp 实现相同的功能。到目前为止,我发现 set-difference 有效:

(set-difference '(1 2 3 4 5 6 7) '(4 5 6))

还有其他解决方案吗?

In haskell, [1,2,3,4,5,6,7] \\ [4,5,6] will return [1,2,3,7]. Now i want to implement the same function using clisp. Up to now i find set-difference works :

(set-difference '(1 2 3 4 5 6 7) '(4 5 6))

Are there any other solution ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

巴黎盛开的樱花 2024-12-12 01:32:56

以下是 haskell 库源代码的相关部分。也许你可以直接翻译这些定义。我认为它没有使用 Haskell 特有的任何东西。

(来源来自 http://haskell。 org/ghc/docs/latest/html/libraries/base/src/Data-List.html

delete                  :: (Eq a) => a -> [a] -> [a]
delete                  =  deleteBy (==)

-- | The 'deleteBy' function behaves like 'delete', but takes a
-- user-supplied equality predicate.
deleteBy                :: (a -> a -> Bool) -> a -> [a] -> [a]
deleteBy _  _ []        = []
deleteBy eq x (y:ys)    = if x `eq` y then ys else y : deleteBy eq x ys

(\\)                    :: (Eq a) => [a] -> [a] -> [a]
(\\)                    =  foldl (flip delete)

Here are relevant bits of haskell library source. Maybe you can translate these definitions directly. I don't think it uses anything specific to Haskell.

(the source is from http://haskell.org/ghc/docs/latest/html/libraries/base/src/Data-List.html)

delete                  :: (Eq a) => a -> [a] -> [a]
delete                  =  deleteBy (==)

-- | The 'deleteBy' function behaves like 'delete', but takes a
-- user-supplied equality predicate.
deleteBy                :: (a -> a -> Bool) -> a -> [a] -> [a]
deleteBy _  _ []        = []
deleteBy eq x (y:ys)    = if x `eq` y then ys else y : deleteBy eq x ys

(\\)                    :: (Eq a) => [a] -> [a] -> [a]
(\\)                    =  foldl (flip delete)
落花浅忆 2024-12-12 01:32:56

我不太了解 Common Lisp,所以这里是 Ben 粘贴的代码的方案实现:

(define (difference big small)
  (fold delete big small))

(define (delete x lst)
  (delete-by equal? x lst))

(define (delete-by equal? x lst)
  (if (null? lst) '()
      (receive (y ys) (car+cdr lst)
        (if (equal? x y) ys
            (cons y (delete-by equal? x ys))))))

其中 foldcar+cdr 来自 SRFI 1,并且 receive 来自 SRFI 8


如果我们允许自己使用 SRFI 26cut 形式,那么我们就有了一个看起来更接近 Haskell 版本的解决方案(因为后者至少在两个地方使用柯里化):

(define difference (cut fold delete <...>))
(define delete (cut delete-by equal? <...>))

; Unchanged from the above version
(define (delete-by equal? x lst)
  (if (null? lst) '()
      (receive (y ys) (car+cdr lst)
        (if (equal? x y) ys
            (cons y (delete-by equal? x ys))))))

I don't know Common Lisp that well, so here's a Scheme implementation of the code pasted by Ben:

(define (difference big small)
  (fold delete big small))

(define (delete x lst)
  (delete-by equal? x lst))

(define (delete-by equal? x lst)
  (if (null? lst) '()
      (receive (y ys) (car+cdr lst)
        (if (equal? x y) ys
            (cons y (delete-by equal? x ys))))))

where fold and car+cdr come from SRFI 1, and receive comes from SRFI 8.


If we will allow ourselves the use of SRFI 26's cut form, then we have a solution that looks even closer to the Haskell version (since the latter uses currying in at least two places):

(define difference (cut fold delete <...>))
(define delete (cut delete-by equal? <...>))

; Unchanged from the above version
(define (delete-by equal? x lst)
  (if (null? lst) '()
      (receive (y ys) (car+cdr lst)
        (if (equal? x y) ys
            (cons y (delete-by equal? x ys))))))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文