Since the repl has no multiline support yet, I was trying to define a function in one line like so:
fix = |x, f| { var $cur = x; var $next = f(x); while $cur != $next {$cur = $next; $next = f($cur)}; $cur }
But this did not work.
Is there any way I can achieve a one-liner with current syntax?
~~
Also, it would be nice to be able to omit $next like in python, where I could use an assignment-expression like this:
def fix(x, f):
cur = x
while cur != (cur := f(cur)): continue
return cur
But I guess this would not be a good fit for roc because it makes expressions have side effects of sorts (in particular, in this snippet order of arguments to != matters)
Hi @Lukas Juhrich,
Roc does not support semicolons, I believe your example as a one-liner is not possible, but you can do
fix_one_liner = |x, f| if f(x) == x { x } else { fix_one_liner(f(x), f) }
I am implementing multi line paste in the repl right now though :)
Also, it would be nice to be able to omit $next like in python, where I could use an assignment-expression like this:
Yeah we provide some imperative escape hatches but that seems a bridge too far :smile:
Anton said:
I am implementing multi line paste in the repl right now though :)
Now that's what I call great timing! ;)
Anton said:
Hi Lukas Juhrich,
Roc does not support semicolons, I believe your example as a one-liner is not possible, but you can dofix_one_liner = |x, f| if f(x) == x { x } else { fix_one_liner(f(x), f) }
Oh right we have tail recursion, thanks, I didn't think of that!
Lukas Juhrich has marked this topic as resolved.
Last updated: May 23 2026 at 12:51 UTC