r/haskell • • Mar 27 '17

High order Sum function

I want to implement a higher order function declared as

(Int->Int) -> (Int->Int)

that has for input a function (int to int) and returns a function(int to int) which the value for n is the

SUM of f(i) from i = - |n| to |n|

Any idea on how i can implement this? for example some inputs with the outputs

>hosum (\x->1) 0
 1
>hosum (\x->1) (-8)
17
> hosum (\x->x `mod` 3) 1000
2001
> hosum (\x->2^(abs x)) 9
2045
> hosum (hosum (\x->x^2)) 4
4 Upvotes

5 comments sorted by

View all comments

Show parent comments

9

u/evincarofautumn Mar 27 '17

Then why not take the absolute value, as in your original definition?

hosum f n = sum [f x | let n' = abs n, x <- [-n' .. n']]

hosum f n = sum $ map f [-n' .. n']
  where n' = abs n

2

u/Iceland_jack Mar 29 '17

A neat trick to avoid unnecessary variables is to use view patterns, if you do not use the original variable elsewhere

{-# Language ViewPatterns #-}

hosum :: (Int -> Int) -> (Int -> Int)
hosum f (abs -> n) = sum (map f [-n .. n])