1.3 Using Built-in Operators and Functions🔗ℹ

Shplait includes some of the usual operators and functions on numbers, like + and max, used in the usual way.

> 1 + 2

- Int

3

> max(3, 5)

- Int

5

> 1/0

- Int

quotient: division by zero

Extra parentheses are allowed around an expression.

> (((1) + ((2))))

- Int

3

The type of a function is written with ->. The function’s argument types appear before the arrow, and the function’s result type is after the arrow. If a function only has one argument, the argument type can be written by itself, otherwise the argument types are comma-separated and grouped with parentheses. A function is a value, so if you evaluate just max without calling it, then Shplait will show the type and print that the result is a function:

> max

- (Int, Int) -> Int

#<function:max>

Line breaks and indentation matter in Shplait. A 1 + with 2 on the next line does not count as a use of the + operator:

> 1 +

  2

+: missing expression after operator

An expression with infix operator can be split across lines, as long as the operator appears at the start of the second line, and as long as the second line is more indented than the first.

> 10000

    + 20000

- Int

30000

Continue over even more lines by indenting to match the second line.

> 10000

    + 20000

    + 30000

- Int

60000

Lines and indentation still matter within parentheses. Function-call arguments can be on their own lines, but any argument that starts a new line must be indented the same as other arguments that start on their own lines. Indentation inside of parentheses can start anywhere, and it is not constrained by indentation outside the parentheses; usually, it starts either just after the open parenthesis by continuing on that same line, or it starts on a new line that is indented relative to the preceding line (instead of the preceding parenthesis).

> max(1,

      2)

- Int

2

> max(

    1,

    2

  )

- Int

2

Here are some example uses of other built-in operators and functions, and you can click on any of the operator or function names to jump to the documentation:

> ! #true

- Boolean

#false

> ! #false

- Boolean

#true

> 1 + 2

- Int

3

> 1 - 2

- Int

-1

> 1 * 2

- Int

2

> 1 < 2

- Boolean

#true

> 1 > 2

- Boolean

#false

> 1 == 2

- Boolean

#false

> 1 <= 2

- Boolean

#true

> 1 >= 2

- Boolean

#false

> string_append("a", "b")

- String

"ab"

> "a" == "b"

- Boolean

#false

> string_get("a", 0)

- Char

#{#\a}

Note that some operations work on multiple types. For example, == works on any two values, as long as the two values have the same type. That flexibility and constraint is reflected in the type of == by a symbol placeholder ?a, which you can read as “a type to be picked later.” A specific type is picked for every individual use of ==.

> 1 == 1

- Boolean

#true

> #'a == #'b

- Boolean

#false

> 1 == "a"

typecheck failed: String vs. Int