I\'m trying to carry out the following computation: sum of the squares of integers in the range x:y where (x <= y).
I\'m not sure how to put a constraint to ensur
In GHCI do these steps to see what's going on:
Prelude> let sq x = x^2
Prelude> let list = [1..5]
Prelude> list
[1,2,3,4,5]
Prelude> let listOfSq = map sq list
Prelude> listOfSq
[1,4,9,16,25]
Prelude> sum listOfSq
55
The shorthand would be exactly what Jubobs suggested:
sum $ map (^2) [1..5]
EDIT: To error out when Y is greater than X you can do something like this
sumOfSquares :: Integer -> Integer -> Integer
sumOfSquares x y | y <= x = error "Y must be greater than X"
| otherwise = sum $ map (^2) [x..y]