例如:
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]
我确实有两个问题:
更正后:
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]
答案 0 :(得分:2)
_
定义左侧的 =
是通配符,是模式的一部分。它匹配任何东西,什么都不记得。
_
定义右侧的 =
并不表示值;它向编译器发出信号:“我什么都没有,只想看看应该放哪种类型!”。
这就是编译器告诉您的内容。这不是一个错误,而是一条消息:
* Found hole: _ :: [(Int, [Int])]
即您需要在其中放置上述类型的内容({::
为:“具有类型”)。
此_
被称为类型的孔。每个_
是不同的,唯一的。它们用于类型驱动的开发。