置换haskell

发布于 2025-02-09 07:05:48 字数 503 浏览 2 评论 0原文

我有一个Haskell工作,要求我使用之前写过的其他两个功能编写置换函数,这两个功能正常工作,但我不知道如何将这些2连接到PERM功能,我该如何执行这?

prepend :: Num a => a -> [[a]] -> [[a]]
prepend a l = map (a :) l

delete :: Eq a => a -> [a] -> [a]
delete x [] = []
delete x (y:ys) 
    | x == y    = delete x ys
    | otherwise = y : delete x ys

perm的声明应为:perm :: num ad⇒[a]→[[a]]

我尝试的内容:

perm [] = [[]]
perm p = [prepend x xs |  x <- p, xs <- perm (delete x p)]

I have a Haskell work which ask me to write a permutation function using 2 other functions prepend and delete I wrote before, those two functions works fine but I somehow don't know how to connect those 2 into the perm function, how can i do this?

prepend :: Num a => a -> [[a]] -> [[a]]
prepend a l = map (a :) l

delete :: Eq a => a -> [a] -> [a]
delete x [] = []
delete x (y:ys) 
    | x == y    = delete x ys
    | otherwise = y : delete x ys

The declaration of perm should be : perm :: Num a ⇒ [a] → [[a]]

What I tried:

perm [] = [[]]
perm p = [prepend x xs |  x <- p, xs <- perm (delete x p)]

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

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

发布评论

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

评论(1

白鸥掠海 2025-02-16 07:05:48

只需使用而不是prepend

perm p = [x:xs | ...]

Just use : instead of prepend.

perm p = [x:xs | ...]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文