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?
The first problem is that
map2just discards the function it’s given.map1is just the standardmapfunction, so you don’t need to define it.map2can be defined properly as:The second problem is that you haven’t supplied
map1andmap2with any arguments in your definition ofcalc. Is something like this what you want?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
calcis wrong. I suggest you think about why this can’t work — in particular, what if I say a isStringand b is()? You can try removing the type signature ofcalcand entering:t calcinto GHCi to find out what the type GHC infers forcalcis to get a head-start on correcting it.