Parallel Assignments and Fuze
-
Posting this here for other beginners in case it helps.
I was working on a crypto program and trying to port an example from Python to handle GCD (Greatest Common Divisor) when I hit snag with my coding skills. Python has a thing called 'parallel assignment' and it's magical powers look like this:
def gcd(a,b) // return the GCD for a and b using Euclid's algorithm. while a != 0: a, b = b % a, a return b
Ok, that looks easy but conceptually stumped me. I got an alternate version to work, but it used recursion and was also clear as mud (turn on debug to see that in action below).
function gcd(a, b) if b == 0 then //print("DEBUG: L1 : a = ", a, " b = ", b, "\n") a = a else //print("DEBUG: L1 : a = ", a, " b = ", b, "\n") a = gcd(b, a % b) endif return a
I wanted to try porting the original Python version again and finally got this to work. Thanks to a little help from https://en.wikipedia.org/wiki/Assignment_(computer_science)
function gcd(a, b) int t while b != 0 loop //print("DEBUG: L1 : a = ", a, " b = ", b, "\n") t = a a = b % a b = t repeat return a
Question: is this the preferred way to approach the concept of parallel assignment in Fuze? I'm asking because I feel like others will see examples and possibly land down this rabbit hole as well.
Apologies if this is a dumb question. Thanks for the advice.
-AlienDjinn
-
I’m no expert, but I think your solution looks reasonable. Just one question: in the python example you loop while a != 0, but your code loops while b != 0. Any reason for that? It seems like it could potentially affect the answer depending on a and b’s initial value.
Edit: OK, now I noticed that you return a instead of b, which confuses me further... But If the function returns the correct values, then I guess it works?
-
This post is deleted! -
I had to just lookup what parallel assignment even was!!
So, Fuze doesn't support it directly but if I'm understanding the article correctly then wouldn't this work?
function gcd(a,b) // return the GCD for a and b using Euclid's algorithm. while a != 0 loop var t = a a = b % a b = t repeat return b
I haven't tested it - and, note, it's almost identical to your verstion
-