Squiggle logoSquiggle
Guides

Control Flow

This page documents control flow. Squiggle has if/else statements, but not for loops. Instead of for loops, you can use reduce/map constructs, which are also documented here.

Conditionals

If-else

if condition then result else alternative

If-else as a ternary operator

test ? result : alternative;

Tips and tricks

Use brackets and parenthesis to organize control flow

or

This is overkill for simple examples becomes useful when the control conditions are more complex.

Save the result to a variable

Assigning a value inside an if/else flow isn't possible:

x = 10
y = 20
if x == 1 then {
  y = 1
} else {
  y = 2 * x
}

Instead, you can do this:

Likewise, for assigning more than one value, you can't do this:

y = 0
z = 0
if x == 1 then {
  y = 2
} else {
  z = 4
}

Instead, do:

For loops

For loops aren't supported in Squiggle. Instead, use a map or a reduce function.

Instead of:

xs = [];
for (i = 0; i < 10; i++) {
  xs[i] = f(x);
}

do:

On this page