From 54a3d6697eab419f138c1ed9a5c8de9787389d92 Mon Sep 17 00:00:00 2001 From: Snorre Date: Thu, 20 Mar 2025 12:49:16 +0100 Subject: [PATCH] expanded readme --- README.md | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 97 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 299b783..98dbf02 100644 --- a/README.md +++ b/README.md @@ -100,8 +100,8 @@ x := 5 # Global scope print(x) # prints 5 if true then # Define a new scope - x := 5000 # Shadow the value of x - print(x) # prints 5000 + x := 5000 # Shadow the value of x + print(x) # prints 5000 end # Scope ends here print(x) # prints 5 @@ -121,6 +121,21 @@ end print(x) # prints 500 ``` +### Set assignment + +You can unpack a set by assigning it to another set, eg + +``` +[x,y] = [5,6] + +print(x) # 5 +print(y) # 6 +``` + +The sets must have the same length, otherwise the expression will error. + +This is useful for functions such as `head` + ## If-else If expressions are defined as such @@ -182,6 +197,86 @@ loop end ``` +In order to avoid the system hanging you can use the `break` keyword to stop a loop. + +``` +x := 5 + +loop + if x = 0 then + break + end + + x <- x-1 +end +``` + +## Functions + +Functions in openbirch allow you to define operations on a set of inputs. Functions are a first class type and can be defined as a set of arguments and a body. + +`args -> body` + +Heres a few examples: + +``` +# A function that takes 1 argument and returns the argument multiplied by 2 +f := x -> x*2 + +f(4) # 8 +``` + +If you want to use multiple arguments then supply a set as the argument + +``` +# A function that takes 2 arguments and adds them together +f := [x,y] -> x+y + +f(2,3) # 5 +``` + +Assigning a function to a name is the most common way of using them. This can be done in 2 ways + +``` +# Define f with a value that is a function +f := x -> x*2 + +# This does the same, but is more intuitive for people from a math background +f(x) := x*2 +``` + +### Currying + +Functions can return other functions. This creates a *closure*, that captures the variables passed to it. + +``` +add := x -> y -> x+y + +add(2)(3) # this evaluates to 5 + +add2 := add(2) # This returns a closure where x is defined as 2 + +print(add2) # this prints ( (x: 2) => ([y] -> x+y)) + # What this shows is a closure that captures "x: 2", + # where inside is another function that defines y + +add2(5) # this is the same as add(2)(5) and returns 7. +``` + +### Built-in functions + +A few functions are defined by default. They provide additional functionality +that has yet to be implemented into the language, such as syntax for indexing sets +and getting their length. + +- `length(Set)` Returns the length of the given set +- `get(Set,Constant)` Returns the value at the given index in the set +- `head(Set)` Returns another Set consisting of `[head, [tail]]` where `head` is the first element in the set, and `tail` is all elements except the first element. +- `map(Function, Set)` Returns a new set where every value has had the function applied to it. +- `unset(String)` Undefines the given variable +- `set_float_precision(Constant)` Sets the number of bits used for numbers. Default is 128bits +- `get_float_precision()` Returns the current float precision. If a constant is given as the argument it will return the number of bits used for that constant. + # Running ## Linux