~melchizedek6809/nujel.net

5278ceaf0afbfbf0ba4a4b4ff43cea559cc2032e — Ben (X13/Arch) 2 years ago f6be3a3
Minor changes, mainly to the example code
M content/language/chapters/1-1_parentheses.html => content/language/chapters/1-1_parentheses.html +16 -6
@@ 6,13 6,23 @@ date: "2022-08-31"
<h1>Parentheses</h1>
<p>Nujel uses the S-Expression notation as used by languages such as <i>Lisp</i>, <i>Scheme</i> and <i>Clojure</i>, but with one difference: Nujel defaults to using brackets instead of parentheses. This is mainly because brackets are much easier to type with a default US keyboard layout. You can still use parentheses if you must, since the two are completely interchangeable.</p>
{{ [component :Nujel #nil
"[+ 1 2] ; => 3
(+ 1 2) ; => 3"] }}
"[+ 1 2]
3

(+ 1 2)
3"] }}

<p>Dotted pairs are also supported:</p>
{{ [component :Nujel #nil
"[car '[a . b]] ; => a
[cdr '[a . b]] ; => b
[car [cons 1 2]] ; => 1
[cdr [cons 1 2]] ; => 2"] }}
"[car '[a . b]]
a

[cdr '[a . b]]
b

[car [cons 1 2]]
1

[cdr [cons 1 2]]
2"] }}
<p>And as you can see Nujel uses <code>car</code> and <code>cdr</code> to access the first or rest parts of lists. To build a new pair you can use <code>cons</code>.</p>

M content/language/chapters/1-2_comments.html => content/language/chapters/1-2_comments.html +1 -1
@@ 8,7 8,7 @@ date: "2022-08-31"
{{ [component :Nujel #nil
"; A single semicolon comments out everything until the next line
(+ 1 #;2 3) ; You can use #; to comment out the following form, should be SRFI-62 compatible!
; => 4
4
#|
 | Nujel also allows for SRFI-30 like nested Multi-line comments
 |#

M content/language/chapters/1-3_numbers.html => content/language/chapters/1-3_numbers.html +14 -13
@@ 9,34 9,35 @@ date: "2022-08-31"

{{ [component :Nujel #nil
"9 ; probably not that suprising
; => 9
9

100,0; possible, but probably shouldn't be commited that way
; => 1000
100,0 ; possible, but probably shouldn't be commited that way
1000

1,0,0,0; also a possibility...
; => 1000
1,0,0,0 ; also a possibility...
1000

1,000 ; much better!
; => 1000
1000

1_000 ; Underscore is also workable, although mostly preferrable for non decimal literals
; => 1000
1000

#b10000 ; This way we can write binary literals
; => 16
16

#b0001_0000 ; Especially here does it become useful that we can use _ and , to split our literal wherever we choose
; => 16
16

#x12_34 ; Also helps for hex literals
; => 4660
4660

#o10 ; Octal literals are also possible
; => 8
8

0x123 ; Using an 0x prefix does NOT work and results in a read error being thrown
; => :read-error
:read-error

-100 ; You can also write negative numbers"] }}
-100 ; You can also write negative numbers
-100"] }}
</pre>
\ No newline at end of file

M content/language/chapters/1-4_arithmetic.html => content/language/chapters/1-4_arithmetic.html +11 -11
@@ 16,25 16,25 @@ date: "2022-08-31"

{{ [component :Nujel #nil
"[+ 1 2 3 4] ; You can add as many numbers as you want
; => 10
10

[+ 1 2 [+ 3 4]]
; => 10
10

[+ [+ 1 2] [+ 3 4]]
; => 10
10

[+ 1 -2] ; You can also use negative numbers in additions
; => -1
-1

[+ 1 [- 2]] ; - can also be used to negate numbers
; => -1
-1

[+ 1 [- [+ 1 1]]] ; Results of a calculation can also be negated
; => -1
-1

[+ 1 \"drei\"] ; You can only calculate with numbers
; => :type-error"] }}
:type-error"] }}

<h3>2. Exceptions</h3>
<p>Dividing through zero generates an exception, which is probably not that suprising, this however also extends to floating point operations where NaN as well as +/-Inf are invalid values that Nujel will not create, instead raising an Exception.</p>


@@ 46,10 46,10 @@ date: "2022-08-31"

{{ [component :Nujel #nil
"[/ 10 0]
; => :float-inf
:float-inf

[div/int 10 0]
; => :divide-by-zero"] }}
:divide-by-zero"] }}

<h3>3. Conversion</h3>
<p>Nujel also provides a couple of functions for converting between the different numeric formats.</p>


@@ 57,8 57,8 @@ date: "2022-08-31"
<p>It will however implicitly convert fixnum's to flownum's whenever that seems to be the least suprising choice.</p>
{{ [component :Nujel #nil
"[int 1.1]
; => 1
1

[float 1]
; => 1.0"] }}
1.0"] }}
</pre>

M content/language/chapters/1-5_symbols-keywords.html => content/language/chapters/1-5_symbols-keywords.html +7 -7
@@ 18,13 18,13 @@ date: "2022-09-07"
; => :unbound-variable

'asd ; By quoting a symbol this implicit look up can be disabled
; => asd
asd

[string->symbol \"asd\"] ; You can also turn any old string into a symbol, this is sometimes called interning a string
; => asd
asd

[== 'asd [string->symbol \"asd\"]] ; The two values actually point to the same underlying data structure
; => #t"] }}
#t"] }}

<box-wrap box-color="yellow">
    <h3>Implementation detail</h3>


@@ 36,16 36,16 @@ date: "2022-09-07"
<p>By pre- or suffixing a symbol with a <code>:</code> the reader will return a keywords.</p>
{{ [component :Nujel #nil
":asd ; A keyword
; => :asd
:asd
    
asd: ; Another keyword
; => asd:
asd:
    
[= asd: :asd] ; It doesn't matter where we put the colon
; => #t
#t
    
[= :asd [symbol->keyword 'asd]]
; => #t"] }}
#t"] }}

<h3>3. Usage</h3>
<p>Regarding their usage, keywords should be used when the keyword itself has a useful meaning, symbols should be used to label other values which could work just as well with another symbol associated.</p>

M content/language/chapters/1-6_quote-quasiquote.html => content/language/chapters/1-6_quote-quasiquote.html +5 -5
@@ 12,13 12,13 @@ date: "2022-08-31"
<p>Quote can not only be used to inhibit the implicit behaviour of symbol resolution, but can also stop expressions from being evaluated and instead being passed along as simple lists. This allows for the easy inclusion of literal lists.</p>
{{ [component :Nujel #nil
"'a ; You can quote symbols
; => a
a

'[1 2 3] ; Or forms, making them plain lists
; => [1 2 3]
[1 2 3]

'(1 2 3) ; With parentheses or brackets
; => [1 2 3]"] }}
[1 2 3]"] }}

<h3>2. Quasiquote</h3>
<p>What is one supposed to do if one wants to quote only parts of an expression though? You could build it up using <code>cons</code>,<code>list</code> and quote but there is a more convenient way for that: <code>quasiquote</code>.</p>


@@ 29,10 29,10 @@ date: "2022-08-31"
</box-wrap>
{{ [component :Nujel #nil
"`[1 2 ~[+ 1 1 1]] ; To unquote you can use a tilde
; => [1 2 3]
[1 2 3]

`[1 ~@[list 2 3]] ; And ~@ for unquote-splicing
; => [1 2 3]"] }}
[1 2 3]"] }}
<box-wrap box-color="yellow">
    <h3>Implementation detail</h3>
    <p>Unlike in other Lisp/Scheme implementations Quasiquote is just a regular macro, this shouldn't make a difference in most cases but might trip you up if you delve deeper into the runtime.</p>

M content/language/chapters/1-7_variables.html => content/language/chapters/1-7_variables.html +8 -5
@@ 10,11 10,13 @@ date: "2022-09-02"
<p>You can define new variables using <code>def</code> and give old variables a new value using <code>set!</code>.</p>
{{ [component :Nujel #nil
"my-temp ; You can access a variables value by evaluating the symbol
; => :unbound-variable
:unbound-variable

[def my-temp 123] ; Of course it needs to be defined first
; => 123
123

my-temp
; => 123"] }}
123"] }}

<h3>2. Setting variables</h3>
<p>If you wish to change the value already associated with a given symbol, you can use <code>set!</code> to achieve that.</p>


@@ 22,7 24,7 @@ my-temp
"[def my-temp 123] ; Gotta define it first before we can use set!
[set! my-temp 234]
my-temp
; => 234"] }}
234"] }}

<h3>3. Let blocks</h3>
<p>You can use <code>let</code> to create a new context for assignments, you still have access to all symbols defined in parent contexts.</p>


@@ 34,4 36,5 @@ my-temp
{{ [component :Nujel #nil
"[let [[new-temp 123]]
     [println new-temp]] ; => 123
new-temp ; => :unbound-variable"] }}
new-temp
:unbound-variable"] }}

M content/language/chapters/1-8_functions.html => content/language/chapters/1-8_functions.html +10 -6
@@ 10,10 10,12 @@ date: "2022-09-01"
<p>Defining a function is best done with the <code>[defn]</code> macro if it is named, or <code>[fn]</code> for anonymous functions.</p>
{{ [component :Nujel #nil
"[defn double [α] [* 2 α]]
[double 2] ; => 4
[double 2]
4

[def double [fn [b] [* 2 b]]]
[double 2] ; => 4"] }}
[double 2]
4"] }}

<h3>2. Defining functions with a rest argument</h3>
<p>In order to define a function that has a certain number of named arguments, and then a final catch-all argument you can use the dotted pair notation, just like in <i>Scheme</i>.</p>


@@ 21,14 23,14 @@ date: "2022-09-01"
"[defn multiply-vals [val . l]
      [map l [fn [v] [* v val]]]]
[multiply-vals 2 1 2 3]
; => [2 4 6]"] }}
[2 4 6]"] }}

<h3>3. Defining a functions with any amount of arguments</h3>
<p>If you want to define a function that can take an arbitrary amount of arguments, you can just omit the brackets surrounding the argument list, this is just like you may be used from <i>Scheme</i>.</p>
{{ [component :Nujel #nil
"[defn my-list l l]
[my-list 1 2 3 4]
; => [1 2 3 4]"] }}
[1 2 3 4]"] }}

<h3>4. Documenting functions</h3>
<p>You can document your functions behaviour by having your functions start with a string literal (don't worry it will be optimized out of the final bytecode).</p>


@@ 38,7 40,8 @@ date: "2022-09-01"
"[defn double [α]
      \"Return α multiplied by 2\"
      [* 2 α]]
[double 2] ; => 4"] }}
[double 2]
4"] }}

<h3>5. Function decorators</h3>
<p>In order to tell the compiler some additional information about the function you are declaring, you can add keywords to the beginning of your function body.</p>


@@ 48,5 51,6 @@ date: "2022-09-01"
      :inline
      \"Return α multiplied by 2\"
      [* 2 α]]
[double 2] ; => 4
[double 2]
4
;; Doesn't really make much difference here, but helps especially with some simple predicates like [zero?]"] }}
\ No newline at end of file

M content/language/chapters/1-9_modules.html => content/language/chapters/1-9_modules.html +1 -0
@@ 42,6 42,7 @@ date: "2022-08-31"
[defn test []
      :export  ; The easiest way to export is to decorate your [defn] forms with the :export keyword, it will export the function with the name specified
      [println \"Test has been called!\"]]

[defn test-2 []
      :export-as test-zwei  ; You can also use the :export-zwei decorator if you want different internal and external names
      [println \"Test has been called!\"]]

M content/reference/index.html => content/reference/index.html +1 -1
@@ 22,6 22,6 @@ date: "2022-09-13"
    [-> [symbol-table]
        [map resolve]
        [for-each [fn [a] [tree/set! cats [closure/cat a] #t]]]]
    [join [map [tree/keys cats] [fn [a] [component :CategoryNavigation @[:category a]]]] ""]
    [join [map [sort [tree/keys cats] [fn [a b] [< [string a] [string b]]]] [fn [a] [component :CategoryNavigation @[:category a]]]] ""]
}}
</div>
\ No newline at end of file