汇总和分组

时间:2019-04-24 13:23:01

标签: haskell

例如:

db = [(1,20),(1,12),(1,28),(2,13),(2,37),(4,11),(4,4),(4,5),(4,10)]被给出。

结果:

groupandagg db (+) [(1,60),(2,50),(4,30)]

我们必须首先通过函数获得以下列表:

[ (1[201228]) (2[1337]) (4[114510]) ]

我已经完成了第一个程序:

main :: IO ()    -- This says that main is an IO action.
main = return () -- This tells main to do nothing

makekgrl :: [(Int,Int)]->[(Int,[Int])]
makekgrl []= []
makekgrl (a,c):[] = [(a,c)]
makekgrl ((a,c):(b,d):_)
     | a==b  = makekgrl ([(a,c:d:[]):_)
     | otherwiese = (a,c):makekgrl((b,d):_)

但是此行发生一个错误:

 makekgrl []= []

错误


E:\Haskell\Uebungsblatt_2_Aufgabe_1_(from_reddit).hs:8:13: error:
    parse error on input `='
    Perhaps you need a 'let' in a 'do' block?
    e.g. 'let x = 5' instead of 'x = 5'
  |
8 |  makekgrl []= []

  |             ^
[Finished in 0.5s]

我确实有两个问题:

  1. 为什么会发生错误?
  2. 如何使用我已经完成的程序来获得以上结果?

更正后:

main :: IO ()    -- This says that main is an IO action.
main = return () -- This tells main to do nothing

--the second primnumer Function from leture


makekgrl :: [(Int,Int)]->[(Int,[Int])]
makekgrl [] = []
makekgrl [(a,c)] = [(a,c)]
makekgrl ((a,c):(b,d):_)
    |a==b = makekgrl ((a,c:d:[]):_)
    |otherwiese = [(a,c)] ++ makekgrl((b,d):_)

错误

E:\Haskell\Uebungsblatt_2_Aufgabe_1_(from_reddit).hs:11:34: error:
    * Found hole: _ :: [(Int, [Int])]
    * In the second argument of `(:)', namely `_'
      In the first argument of `makekgrl', namely `((a, c : d : []) : _)'
      In the expression: makekgrl ((a, c : d : []) : _)
    * Relevant bindings include
        d :: Int
          (bound at E:\Haskell\Uebungsblatt_2_Aufgabe_1_(from_reddit).hs:10:20)
        b :: Int
          (bound at E:\Haskell\Uebungsblatt_2_Aufgabe_1_(from_reddit).hs:10:18)
        c :: Int
          (bound at E:\Haskell\Uebungsblatt_2_Aufgabe_1_(from_reddit).hs:10:14)
        a :: Int
          (bound at E:\Haskell\Uebungsblatt_2_Aufgabe_1_(from_reddit).hs:10:12)
        makekgrl :: [(Int, Int)] -> [(Int, [Int])]
          (bound at E:\Haskell\Uebungsblatt_2_Aufgabe_1_(from_reddit).hs:8:1)
      Valid hole fits include
        mempty :: forall a. Monoid a => a
          with mempty @[(Int, [Int])]
          (imported from `Prelude' at E:\Haskell\Uebungsblatt_2_Aufgabe_1_(from_reddit).hs:1:1
           (and originally defined in `GHC.Base'))
   |
11 |     |a==b = makekgrl ((a,c:d:[]):_)

   |                                  ^

E:\Haskell\Uebungsblatt_2_Aufgabe_1_(from_reddit).hs:12:6: error:
    * Variable not in scope: otherwiese :: Bool
    * Perhaps you meant `otherwise' (imported from Prelude)
   |
12 |     |otherwiese = [(a,c)] ++ makekgrl((b,d):_)

   |      ^^^^^^^^^^

E:\Haskell\Uebungsblatt_2_Aufgabe_1_(from_reddit).hs:12:45: error:
    * Found hole: _ :: [(Int, Int)]
    * In the second argument of `(:)', namely `_'
      In the first argument of `makekgrl', namely `((b, d) : _)'
      In the second argument of `(++)', namely `makekgrl ((b, d) : _)'
    * Relevant bindings include
        d :: Int
          (bound at E:\Haskell\Uebungsblatt_2_Aufgabe_1_(from_reddit).hs:10:20)
        b :: Int
          (bound at E:\Haskell\Uebungsblatt_2_Aufgabe_1_(from_reddit).hs:10:18)
        c :: Int
          (bound at E:\Haskell\Uebungsblatt_2_Aufgabe_1_(from_reddit).hs:10:14)
        a :: Int
          (bound at E:\Haskell\Uebungsblatt_2_Aufgabe_1_(from_reddit).hs:10:12)
        makekgrl :: [(Int, Int)] -> [(Int, [Int])]
          (bound at E:\Haskell\Uebungsblatt_2_Aufgabe_1_(from_reddit).hs:8:1)
      Valid hole fits include
        mempty :: forall a. Monoid a => a
          with mempty @[(Int, Int)]
          (imported from `Prelude' at E:\Haskell\Uebungsblatt_2_Aufgabe_1_(from_reddit).hs:1:1
           (and originally defined in `GHC.Base'))
   |
12 |     |otherwiese = [(a,c)] ++ makekgrl((b,d):_)

   |                                             ^
[Finished in 0.7s]

1 个答案:

答案 0 :(得分:2)

_定义左侧的

=通配符,是模式的一部分。它匹配任何东西,什么都不记得。

_定义右侧的

=并不表示值;它向编译器发出信号:“我什么都没有,只想看看应该放哪种类型!”。

这就是编译器告诉您的内容。这不是一个错误,而是一条消息:

    * Found hole: _ :: [(Int, [Int])]

即您需要在其中放置上述类型的内容({::为:“具有类型”)。

_被称为类型的孔。每个_是不同的,唯一的。它们用于类型驱动的开发

相关问题