What????
First of all, you should just use length
instead of genericLegth
.
λ > let xs = [1..6] in sum xs / length (drop xs)
<interactive>:7:42: error:
• Couldn't match expected type ‘Int’ with actual type ‘[a1]’
• In the first argument of ‘drop’, namely ‘xs’
In the first argument of ‘length’, namely ‘(drop xs)’
In the second argument of ‘(/)’, namely ‘length (drop xs)’
The problem is that drop
takes two input arguments, not 1 as you have given it in all three examples (the last of which is just wrong).
λ > :t drop
drop :: Int -> [a] -> [a]
Fixing that problem, you get another:
λ > let xs = [1..6] in sum xs / length (drop 2 xs)
<interactive>:9:20: error:
• No instance for (Fractional Int) arising from a use of ‘/’
• In the expression: sum xs / length (drop 2 xs)
In the expression: let xs = [1 .. 6] in sum xs / length (drop 2 xs)
In an equation for ‘it’:
it = let xs = ... in sum xs / length (drop 2 xs)
Now, you have a problem with the /
operator not being usable with Int
.
The solution is:
let xs = [1..6] in fromIntegral (sum xs) / fromIntegral (length $ drop 2 xs)
5.25
The reason this does not work:
let xs = [1..6] in sum xs / length $ drop 2 xs
is because it gets desugared to:
let xs = [1..6] in (sum xs / length) (drop 2 xs)
which does not make sense.