<commit_before>---
navhome: /docs/
sort: 12
next: true
title: The subject
---

The subject
===========

Now we're going to cover the boiler plate that we skimmed over earlier.

    :-  %say  |=  *  
    :-  %noun
    =<  (sum [1.000 2.000])

The first rune, `:-` (colhep, aka
[:cons](../../hoon/twig/col-cell/hep-cons/)), constructs the 2-element cell
that will be our program. The first element, `%say`, tells the
interpreter what to produce--in this case a value.

The second element is `|=`, which we know produces a function. `|=`'s
first child is its argument(s), which in this case is any noun (`*`).
Its second child is the remainder of the program.

Similarly, the rest of the program (which we construct with another
`:-`) is a cell of the literal `%noun`, which tells the shell that we're
producing a value of type `noun`, and the code that we run to actually
produce our value of the type `noun`.

`=<` ([tisgal](../../hoon/twig/tis-flow/gal-rap/)) is a rune that takes two
children. The second child is the context against which we run the first
child. So in this case, we are running the expression
`(sum [1.000 2.000])` against everything contained within the `|%`. In
Hoon, we call the code executed the "formula" and its context the
"subject".

    ::::::::::::::::::::::::::::::
    =<  (sum [1.000 2.000])     :: formula
    ::::::::::::::::::::::::::::::
    |%                          ::
    ++  three                   ::
      |=  a/@                   ::
      =|  b/@                   ::
      |-  ^-  @u                ::
      ?:  (lth a b)             ::
        0                       ::
      (add b $(b (add 3 b)))    ::
    ::                          ::
    ++  five                    ::
      |=  a/@                   ::  subject
      =|  b/@                   ::
      |-  ^-  @                 ::
      ?:  (lte a b)             ::
        0                       ::
      ?:  =((mod b 3) 0)        ::
        $(b (add b 5))          ::
      (add b $(b (add b 5)))    ::
    ::                          ::
    ++  sum                     ::
      |=  {a/@u b/@u}           ::
      (add (five a) (three b))  ::
    --                          ::
    ::::::::::::::::::::::::::::::

In nearly every language there is a similar concept of a "context" in
which expressions are executed. For example, in C this includes things
like the call stack, stack variables, and so on.

Hoon is unique in that this context is a first-class value. Scheme
allows a sort of reification of the context through continuations, and
some may see a parallel to Forth's stack, but Hoon takes the
concept one step further.

Our starting subject is the [standard library](../../hoon/library), which is
defined in `/arvo/hoon.hoon` and `/arvo/zuse.hoon`. This is where
functions like `add` are defined. When we define a core with `|%`, we
don't throw away the subject (i.e. the standard library); rather, we
stack the new core on top of the old subject so that both are
accessible.

**Exercises**:

-   Pass `++sum` its arguments (`2.000` and `3.000`) from the
    commandline.

-   Comment out all of the arms of the `|%`. Now add another arm and
    call it `++add`, have it accept two arguments and procduce 42<commit_msg>change commandline to command line<commit_after>---
navhome: /docs/
sort: 12
next: true
title: The subject
---

The subject
===========

Now we're going to cover the boiler plate that we skimmed over earlier.

    :-  %say  |=  *  
    :-  %noun
    =<  (sum [1.000 2.000])

The first rune, `:-` (colhep, aka
[:cons](../../hoon/twig/col-cell/hep-cons/)), constructs the 2-element cell
that will be our program. The first element, `%say`, tells the
interpreter what to produce--in this case a value.

The second element is `|=`, which we know produces a function. `|=`'s
first child is its argument(s), which in this case is any noun (`*`).
Its second child is the remainder of the program.

Similarly, the rest of the program (which we construct with another
`:-`) is a cell of the literal `%noun`, which tells the shell that we're
producing a value of type `noun`, and the code that we run to actually
produce our value of the type `noun`.

`=<` ([tisgal](../../hoon/twig/tis-flow/gal-rap/)) is a rune that takes two
children. The second child is the context against which we run the first
child. So in this case, we are running the expression
`(sum [1.000 2.000])` against everything contained within the `|%`. In
Hoon, we call the code executed the "formula" and its context the
"subject".

    ::::::::::::::::::::::::::::::
    =<  (sum [1.000 2.000])     :: formula
    ::::::::::::::::::::::::::::::
    |%                          ::
    ++  three                   ::
      |=  a/@                   ::
      =|  b/@                   ::
      |-  ^-  @u                ::
      ?:  (lth a b)             ::
        0                       ::
      (add b $(b (add 3 b)))    ::
    ::                          ::
    ++  five                    ::
      |=  a/@                   ::  subject
      =|  b/@                   ::
      |-  ^-  @                 ::
      ?:  (lte a b)             ::
        0                       ::
      ?:  =((mod b 3) 0)        ::
        $(b (add b 5))          ::
      (add b $(b (add b 5)))    ::
    ::                          ::
    ++  sum                     ::
      |=  {a/@u b/@u}           ::
      (add (five a) (three b))  ::
    --                          ::
    ::::::::::::::::::::::::::::::

In nearly every language there is a similar concept of a "context" in
which expressions are executed. For example, in C this includes things
like the call stack, stack variables, and so on.

Hoon is unique in that this context is a first-class value. Scheme
allows a sort of reification of the context through continuations, and
some may see a parallel to Forth's stack, but Hoon takes the
concept one step further.

Our starting subject is the [standard library](../../hoon/library), which is
defined in `/arvo/hoon.hoon` and `/arvo/zuse.hoon`. This is where
functions like `add` are defined. When we define a core with `|%`, we
don't throw away the subject (i.e. the standard library); rather, we
stack the new core on top of the old subject so that both are
accessible.

**Exercises**:

-   Pass `++sum` its arguments (`2.000` and `3.000`) from the
    command line.

-   Comment out all of the arms of the `|%`. Now add another arm and
    call it `++add`, have it accept two arguments and procduce 42
{'fraction_non_alphanumeric': 0.14764687788372807, 'fraction_numerical': 0.01353429713934174, 'mean_word_length': 1.8647577092511014, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5653200', 'n_tokens_mistral': 1982, 'n_tokens_neox': 1819, 'n_words': 800}