Search code examples
listhaskellsummap-functionhigh-order-component

Haskell code to compute sum of squares of negative integers using foldr function


I am new to haskell code. I tried to compute the sum of squares of negative integer in a list using foldr high order.

  sumsq :: Int -> Int
  sumsq n = foldr op 0 [1..n]
  where op x y = x*x + y

Please help to explain each line of code and give any solution if error in this code


Solution

  • When using "where", important to follow the indentation rule. Here lambda will be appropriate

    sumsq n = foldr (\x y -> x*x + y) 0 [1..n]