I have the following function developed in Haskell, it performs an avg of 3 entered notes and depending on the condition it counts the elements of the list (using length), the first output of the tuple should be count( ) when the condition >= 4 and the second count( ) when it is < 4:
funcion1::[(String, Int,Int,Int)]->(Int,Int)
funcion1 ((nombre,nota1,nota2,nota3):xs) = ( if (nota1+nota2+nota3) `div` 3 >=4 then 1 + funcion1 xs else funcion1 xs ,
if (nota1+nota2+nota3) `div` 3 <4 then 1 + funcion1 xs else funcion1 xs )
However, when entering data in the input, the tuple always returns the same values for both cases (Example: When entering 3 elements with avg >= 4 and only 1 with avg < 4, the output should be (3,1) and currently it is ( 3.3).
You misunderstand the problem. Haskell doesn't use if/else as much as it does in other languages. When working with lists you have to think more about applying filters (
filter
) and other functions that support predicates (what you say conditionals ).We have created the function
avg
that calculates the mean and we have applied it to obtain a list of meansavgs
. Two filters are applied to this list,(>=4)
and(<4)
, to obtain the two sublists whose lengths will give us the count we are looking for.Since the two filters are mutually exclusive (the elements that meet one filter do not meet the other), what we really have is a partition of the list and we can make it a little simpler by applying the function
partition
of the moduleData.List
:All that remains is to make it beautiful by creating a function
maptuple
to make a map in a tuple:Even prettier yet, putting it all together: