从Haskell中的列表中求和偶数的平方

0 dictionary haskell list filter

我想从列表中求和偶数的平方.我试试这个,但显示错误.

sumaDeCuadrados :: [Int] -> Int
sumaDeCuadrados (x:xs) = sumaListAux (map f l) 0 
    where l = filter even (x:xs) 
          f = x * x
sumaDeCuadrados _ = 0
Run Code Online (Sandbox Code Playgroud)

和sumaListAux是一个定义为..的函数.

sumaListAux :: [Int] -> Int -> Int
sumaListAux [] r = r
sumaListAux (x:xs) r = x + sumaListAux xs r
Run Code Online (Sandbox Code Playgroud)

Don*_*art 6

将列表中的偶数的平方相加.

Haskell在某些方面是一种声明性语言,所以你可以声明这些东西意味着什么.

-- declare a list
> let list = [1..10]

-- declare what the even elements of a lsit are
> let evens xs = filter even xs

-- declare what the squares of a list are
> let squares xs = map (^2) xs
Run Code Online (Sandbox Code Playgroud)

总和已经存在,sum.所以现在你的句子:

 sum ??the squares of the even numbers
Run Code Online (Sandbox Code Playgroud)

可以转换为:

> sum . squares . evens $ list
220
Run Code Online (Sandbox Code Playgroud)