Reputation: 417
I give as input a list like : s = [(1,5),(1,4),(3,4)] and i want to take every time the head of this list which will be with recuircive loop 1: (1,5) loop 2: (1,4) and loop 3:(3,4). So am giving to the auxiliary function (head s) in order to take (1,5) and (tail s) in order to take [(1,4),(3,4)]
my code :
statistics :: [(Int,Int)]->(Int,Int,Int,Int,Int)
statistics [] = (0,0,0,0,0)
statistics s = help_statistics s (head s) (tail s) 0 0 0 0 0 0
help_statistics :: [(Int,Int)]->(Int,Int)->[(Int,Int)]->Int->Int->Int->Int->Int->Int->(Int,Int,Int,Int,Int)
help_statistics s (x,y) taill counter matches total_points goal_for goal_against dif
|counter==0 = if taill/=[] then help_statistics s (head taill) (tail taill) counter (matches+1) total_points goal_for goal_against dif
else help_statistics s (head taill) (tail taill) (counter+1) (matches) total_points goal_for goal_against dif
|otherwise = (matches,total_points,goal_for,goal_against,dif)
Upvotes: 0
Views: 68
Reputation: 116174
Look at this part:
if taill/=[]
then help_statistics s (head taill) ..........
else help_statistics s (head taill) .............
The first head
is called on an non-empty list. The second one, on an empty list, triggering the error. You can't access the non-existent head there -- the list is empty!
You should almost never use dangerous functions like head
, tail
, !!
in your code. They are very rarely needed in Haskell. Use instead pattern matching such as
case taill of
(y:ys) -> use y ys -- y is the head, ys is the tail
[] -> something else -- handle the empty list case somehow
In general, turn on warnings with -Wall
, and always fix non-ehsaustive pattern matching is GHC warns about it by adding all the missing cases (GHC suggests those).
Upvotes: 2