Sum in Haskell [closed]

-4

I need to do the summation from i = 1 to n of i ^ n in Haskell. Anyone have an idea or can you help me do it? I'm doing a project and I need that in haskell that I do not have much idea.

    
asked by pepe 18.06.2016 в 18:29
source

3 answers

2

Good evening, a possible solution is this:

sumtoria i n | i==n = i**n
|otherwise = i**n + sumtoria (i+1) n

When i = n makes the power, in the other cases it accumulates the powers while i increases. It is only contemplated by entering values of i less than n. A more elegant solution could be using higher order functions, but that's another topic.

    
answered by 19.06.2016 в 09:11
2

The simplest way:

sumatoria :: Integer -> Integer
sumatoria n = sum [ i^n | i <- [1..n]]
    
answered by 22.06.2016 в 01:33
1

Good morning a possible answer for your serious problem

suma a b | a==b = a**b|otherwise = a**b + suma (a+1) b

Main

suma (2,3) 

I hope you serve

    
answered by 28.08.2017 в 17:09