Reputation: 65
I want to get the minimum values from two mappings and subtract one from the other. I'm really new to Haskell and am pretty embarrassed by my attempt but I wanted to give it a go before asking!
calc :: (a -> b) -> [a] -> Float
calc = a - b
where
a = minimum map1
b = minimum map2
map1 f xs = [f x | x <- xs]
map2 f xs = [square x | x <- xs]
square x = x*x
I'm getting so many errors that I feel like I must be doing it completely the wrong way?
Upvotes: 1
Views: 174
Reputation: 40797
The first problem is that map2
just discards the function it's given. map1
is just the standard map
function, so you don't need to define it. map2
can be defined properly as:
map2 = map square
The second problem is that you haven't supplied map1
and map2
with any arguments in your definition of calc
. Is something like this what you want?
calc :: (a -> b) -> [a] -> Float
calc f xs = a - b
where
a = minimum (map f xs)
b = minimum (map2 xs)
Basically, your problem is that you're declaring parameters but not processing them, or using functions that take parameters without actually specifying them. That doesn't work in any language :)
That's not all, however. Your type for calc
is wrong. I suggest you think about why this can't work — in particular, what if I say a is String
and b is ()
? You can try removing the type signature of calc
and entering :t calc
into GHCi to find out what the type GHC infers for calc
is to get a head-start on correcting it.
Upvotes: 3