diff --git a/book/src/language-tour/assert.md b/book/src/language-tour/assert.md index 878a2425..72e71fc2 100644 --- a/book/src/language-tour/assert.md +++ b/book/src/language-tour/assert.md @@ -1,7 +1,7 @@ # Assert -``` -assert rawdata is SomeType +```gleam +assert rawdata = SomeType ``` Causes the script to fail if the rawdata doesn't match the structure of datumtype diff --git a/book/src/language-tour/blocks.md b/book/src/language-tour/blocks.md index ec5b7c02..6bfe0563 100644 --- a/book/src/language-tour/blocks.md +++ b/book/src/language-tour/blocks.md @@ -2,7 +2,7 @@ If statements -``` +```gleam if condition { } else { @@ -12,7 +12,7 @@ if condition { Case Patterns -``` +```gleam when color is { Green -> "Success." Blue -> "Warning." @@ -22,7 +22,7 @@ when color is { Let bindings with blocks -``` +```gleam let num = -5 let absNum = if num>=0 {num} else {-num} diff --git a/book/src/language-tour/bool.md b/book/src/language-tour/bool.md index bd2476fc..5a3e892a 100644 --- a/book/src/language-tour/bool.md +++ b/book/src/language-tour/bool.md @@ -1,3 +1,19 @@ # Bool -Bools are True or False \ No newline at end of file +Bools are True or False + +There are logical conjunctions (True && True) or disjunctions (True || False). + +```gleam +fn negate(b: Bool)->Bool { + if b { + False + }else{ + True + } +} + +fn and(b: Bool, c: Bool, d: Bool)->Bool{ + b && c && d +} +``` \ No newline at end of file diff --git a/book/src/language-tour/comments.md b/book/src/language-tour/comments.md index 131c7084..3d9627da 100644 --- a/book/src/language-tour/comments.md +++ b/book/src/language-tour/comments.md @@ -1,6 +1,6 @@ # Comments -``` +```gleam // Line comments begin with a double-slash /// Doc comments begin with a triple-slash diff --git a/book/src/language-tour/constants.md b/book/src/language-tour/constants.md index 21fb31ed..f3ef62ca 100644 --- a/book/src/language-tour/constants.md +++ b/book/src/language-tour/constants.md @@ -1 +1,8 @@ # Constants + +Constants (not implemented yet) will be set in the global scope like so: +```gleam +const x = 1 +``` + +Similar to a let binding, but with a const keyword instead. \ No newline at end of file diff --git a/book/src/language-tour/custom-types.md b/book/src/language-tour/custom-types.md index 19c002d1..59df99f9 100644 --- a/book/src/language-tour/custom-types.md +++ b/book/src/language-tour/custom-types.md @@ -5,7 +5,7 @@ Instantiation -``` +```gleam type Color { Red Blue @@ -18,7 +18,7 @@ Enum types can be thought of as the unit of product types. Usage -``` +```gleam fn myFavouriteColor() -> Color { Red } @@ -26,14 +26,14 @@ fn myFavouriteColor() -> Color { ### Product Types -``` +```gleam type MyValue { Name(String) Age(Int) } ``` -``` +```gleam fn eitherNameOrAge(b: Bool) -> MyValue { if b { Name("James") diff --git a/book/src/language-tour/functions.md b/book/src/language-tour/functions.md index 0c5faf50..bcdb0d16 100644 --- a/book/src/language-tour/functions.md +++ b/book/src/language-tour/functions.md @@ -1 +1,43 @@ # Functions + +Functions can be instantiated like so: +```gleam +fn f(x) { + +} +``` +l functions in aiken are pure without side effect, so all functions also must return some value. +A function with no return value (like above) will error. + +Providing arg types: +```gleam +fn f(x: Int) { + +} +``` + +and return types: + +```gleam +fn f(x: Int) -> Int { + +} +``` + +and the last value is implicitly returned: + +```gleam +fn f(x: Int) -> Int { + x + 1 +} +``` + +Functions can be made public in modules so they can be accessed by others + +```gleam +pub fn myFunction(a: List(a)) { + // do something useful... +} +``` + +A \ No newline at end of file diff --git a/book/src/language-tour/int.md b/book/src/language-tour/int.md index 18f49fe7..130de243 100644 --- a/book/src/language-tour/int.md +++ b/book/src/language-tour/int.md @@ -1 +1,15 @@ # Int + +Ints are plutus integers which are arbitrary size. + +So, there is no underflow. Basic arithmetic can be done for O(1) between ints (+,-,*). + +```gleam +// A convenient helper function to get the number 7. +pub fn get7(){ + let x = 3 + let y = 2 + let z = 1 + x*y + z +} +``` \ No newline at end of file diff --git a/book/src/language-tour/list.md b/book/src/language-tour/list.md index 6811f7b8..32656cf4 100644 --- a/book/src/language-tour/list.md +++ b/book/src/language-tour/list.md @@ -1 +1,21 @@ # List + +Aiken lists are plutus linked lists. Accessing by index is O(n). Appending or accessing head is O(1). Grabbing tail is O(1). + +There is no builtin syntax for accessing by index as this is implemented by standard libs. + +Accessing head, tail, or preceding elements can be done by pattern matching. + +```gleam +// this function checks if a list has a sequence of 1 then 2 contained within it. +fn listStuff(a: List(Int)){ + when a is { + [1,2, ..tail] -> True + []->False + _ -> listStuff([2, ..tail]) + } +} +``` + +Helper functions for safely accessing head, tail, are provided in standard lib but are implemented using comprehensions. +It is usually best to use your own comprehensions for efficiency (until the optimiser is better). \ No newline at end of file diff --git a/book/src/language-tour/option.md b/book/src/language-tour/option.md index f33af4ff..a1fae1da 100644 --- a/book/src/language-tour/option.md +++ b/book/src/language-tour/option.md @@ -1 +1,23 @@ # Option + +We define option as a generic type like so + +``` +pub type Option(a) { + None + Some(a) +} +``` + +Then, functions which fail may safely return an optional value. + +``` +pub fn getHead(a: List(a))->a { + when a is { + [a, .._]->Some(a) + []->None + } +} +``` + +An unsafe variant of this function might instead assert that there is a head, and return it.