Stream: beginners

Topic: ✔ Multiple statements in one line


view this post on Zulip Lukas Juhrich (May 03 2026 at 23:37):

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)

view this post on Zulip Anton (May 04 2026 at 11:13):

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) }

view this post on Zulip Anton (May 04 2026 at 11:13):

I am implementing multi line paste in the repl right now though :)

view this post on Zulip Anton (May 04 2026 at 11:14):

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:

view this post on Zulip Lukas Juhrich (May 04 2026 at 11:35):

Anton said:

I am implementing multi line paste in the repl right now though :)

Now that's what I call great timing! ;)

view this post on Zulip Lukas Juhrich (May 04 2026 at 11:36):

Anton said:

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) }

Oh right we have tail recursion, thanks, I didn't think of that!

view this post on Zulip Notification Bot (May 04 2026 at 12:11):

Lukas Juhrich has marked this topic as resolved.


Last updated: May 23 2026 at 12:51 UTC