Is this a valid function. Translating Haskell code to pure maths

106 Views Asked by At

I am currently learning Haskell and I wrote a tail recusive function to find the sum of some list.

I wanted to try and write it out in pure maths, transalating as closely as I can what I did in Haskell to maths, instead of simply using sigma to express the summation.

I would like to know if what I wrote out makes any sense or is valid and if not what would I have to change about it.

Here is what I made:

$$ f(A) = g(A,0) = \sum_{x\in A}x \\ g(A,s) = \begin{cases} s \quad &\text{if } A = \emptyset \\ g(\{a_2,a_3,...,a_n\},a_1+s) \quad &\text{otherwise} \\ \end{cases} \\ \text{where} \\ A \subsetneq \mathbb Z \\ $$

Here's the Haskell code I'm trying to translate.

mySum :: [Int] -> Int
mySum n = mySum' (n) 0
    where
        mySum' (n) x | null n    = x
        mySum' (n:ns) x | otherwise = mySum' (ns) (n + x)

Thanks!

edit: added pattern matching to Haskell for improved readability. Also sightly changed math function to better resemble the Haskell code.