如何在另一个函数的结果列表上使用map?

时间:2018-10-19 05:14:25

标签: haskell

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从每个元素中减去列表的平均值。

基本上我要做的是将sqDiff1的元素平方。

我该如何实现?

1 个答案:

答案 0 :(得分:1)

如果只想平方该函数的差,则不需要函数参数:

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

如果您确实打算拥有一个函数参数,请传递您要使用的参数:

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