These are chat archives for mozart/mozart2

3rd
Oct 2015
Sidhant Bhavnani
@cosmicAsymmetry
Oct 03 2015 09:41
Can someone help me fix my syntax error>
Please.
Sébastien Doeraene
@sjrd
Oct 03 2015 09:42
What's the error and the code?
Sidhant Bhavnani
@cosmicAsymmetry
Oct 03 2015 09:42
fun {MainSum N}
local Sum in
fun {Sum N Acc}
if N==0 then Acc
else
Acc = Acc+(N*N)
N = N-1
end
{Sum N 0}
end
end
Sébastien Doeraene
@sjrd
Oct 03 2015 09:43
Please add triple backquotes around your code to preserve indentation.
Sidhant Bhavnani
@cosmicAsymmetry
Oct 03 2015 09:43
fun {MainSum N}
local Sum in
fun {Sum N Acc}
if N==0 then Acc
else
Acc = Acc+(N*N)
N = N-1
end
{Sum N 0}
end
end
'''
fun {MainSum N}
local Sum in
fun {Sum N Acc}
if N==0 then Acc
else
Acc = Acc+(N*N)
N = N-1
end
{Sum N 0}
end
end
'''
Sébastien Doeraene
@sjrd
Oct 03 2015 09:44
backquotes, like ```
You don't have a syntax error. You are trying to reassign immutable variables. Acc = Acc+(N*N) doesn't mean anything. No more than saying "let x = x + n²" in mathematics. You just can't change the value of variables.
Sidhant Bhavnani
@cosmicAsymmetry
Oct 03 2015 09:46
fun {MainSum N}
    local Sum in
        fun {Sum N Acc}
       if N==0 then Acc
       else 
          Acc = Acc+(N*N)
          N = N-1
        end
        {Sum N 0}
    end
end
Sébastien Doeraene
@sjrd
Oct 03 2015 09:47
You need to call Sum recursively with different values for the parameters N and Acc instead.
Sidhant Bhavnani
@cosmicAsymmetry
Oct 03 2015 09:47
Aah I see.
Thank you.
So, instead of
Acc = Acc+(N*N)
{Sum Acc N*N}