繁体   English   中英

Haskell - 具有foldr函数的组列表元素

[英]Haskell - Group list elements with foldr function

这是在Haskell中的一个赋值。 我们的任务是使用foldr函数定义各种函数。

我们得到了一个类型:

group :: Eq a => [a] -> [[a]]

并被要求定义它:

group [1,2,2,3,4,4,4,5] = [[1], [2,2], [3], [4,4,4], [5]]
group [1,2,2,3,4,4,4,5,1,1,1] = [[1], [2,2], [3], [4,4,4], [5], [1,1,1]]

这是我到目前为止:

group = foldr (\x xs -> if x == head (head xs) then (x : head xs) : xs else (x : []) : (head xs) : xs )

但是当我尝试将其加载到ghci解释器时,我收到以下错误消息:

Couldn't match type `[a0] -> [a]' with `[[a]]'
Expected type: [a] -> [[a]]
  Actual type: [a] -> [a0] -> [a]
In the return type of a call of `foldr'
Probable cause: `foldr' is applied to too few arguments
In the expression:
  foldr
    (\ x xs
       -> if x == head (head xs) then
              (x : head xs) : xs
          else
              (x : []) : (head xs) : xs)
In an equation for `group':
    group
      = foldr
          (\ x xs
             -> if x == head (head xs) then
                    (x : head xs) : xs
                else
                    (x : []) : (head xs) : xs)

如果有人能解释为什么我的代码没有像我期望的那样工作的原因,那将非常感激。 谢谢。

我认为你走在正确的轨道上,所以我会试着把你的想法写得更好一些。 我想说的是:你应该将foldr的第一个参数拉出一个函数并再次进行模式匹配:

group :: Eq a => [a] -> [[a]]
group = foldr f undefined
  where f x []        = undefined
        f x (ys@(y:_):yss)
          | x == y    = undefined
          | otherwise = undefined

这应该做 - 现在你需要放入正确的东西,我把undefined :)

我会稍后回来完成它


好吧我猜你放弃了什么 - 无论如何这里有一个解决方案:

group :: Eq a => [a] -> [[a]]
group = foldr f []
  where f x []        = [[x]]
        f x (ys@(y:_):yss)
          | x == y     = (x:ys):yss
          | otherwise = [x]:ys:yss

以及一些例子:

λ> group []
[]
λ> group [1]
[[1]]
λ> group [1,1]
[[1,1]]
λ> group [1,2,1]
[[1],[2],[1]]
λ> group [1,2,2,3,4,4,4,5]
[[1],[2,2],[3],[4,4,4],[5]]

请注意, f s模式并非详尽无遗(这是没有问题的 - 想想为什么) - 当然,如果你愿意,你可以扩展它(如果你不同意你的group [] = []

只是要提一下,如果我没有错,这就是99个haskell问题中的问题9,可以在这里找到: https//wiki.haskell.org/99_questions/
对于每个问题,它都有一堆解决方案(通常),因为Carsten提供了一个很好的解决方案,你可以去那里看看其他解决方案,这样你就可以通过各种方式获得不同的想法!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM