Pablo H
Pablo H

Reputation: 157

How can I use map on the resulting list of another function?

mean :: (Real a, Fractional b) => [a] -> b
mean xs = (realToFrac(sum xs) / realToFrac(length xs))

sqDiff1 :: (Fractional b, Real b) => [b] -> [b]
sqDiff1 xs = map(subtract (mean xs))xs
sqDiff2 :: Num b => (t -> [b]) -> t -> [b]
sqDiff2 sqDiff2 xs = map(^ 2)(sqDiff2 xs)

sqDiff1 subtracts the mean of a list from each of the elements.

Basically what I'm trying to do is square the elements of sqDiff1.

How can I achieve this?

Upvotes: 0

Views: 76

Answers (1)

molbdnilo
molbdnilo

Reputation: 66371

If you want to square the differences from only that function, you don't need a function parameter:

sqDiff2 :: Real b => [b] -> [b]
sqDiff2 xs = map (^ 2) (sqDiff1 xs)

If you really intended to have a function parameter, pass the one you want to use:

*Main> sqDiff2 sqDiff1 [1,2,3,4,5,6]
[6.25,2.25,0.25,0.25,2.25,6.25]

Upvotes: 1

Related Questions