haskell - How would I write a triangle function recursively? -
so trying write recursive function can input triangle number e.g. 5 , return answer 15. can use output correct answer:
triangle n = n * (n + 1) `div` 2
but how recursively?
triangle 0 = 0 triangle n = n + triangle (n-1)
but why?
Comments
Post a Comment