all repos — min @ ed0ce1f1b5700ab5d9c1c260cb3edf399fd94e8c

A small but practical concatenative programming language.

site/contents/_includes/_learn-quotations.md

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
{@ _defs_.md || 0 @}

Quotations are the most important thing to understand in min. Besides being the data type used for lists, they are also used to delimit blocks of min code that is not going to be immediately executed. 

Consider for example the following min code which returns all the files present in the current folder:

     . ls (ftype 'file ==) filter

The symbol {#link-operator||seq||filter#} takes two quotations as arguments -- the first quotation on the stack is applied to all the elements of the second quotation on the stack, to determine which elements of the second quotation will be part of the resulting quotation. This is an example of how quotations can be used both as lists and programs.

Let's examine this program step-by-step:

{{fdlist => ("dir1" "dir2" file1.txt "file2.txt" "file3.md" "file4.md")}}
{{flist => ("file1.txt" "file2.txt" "file3.md" "file4.md")}}

<table>
  <tr>
    <th>Element</th><th>Stack</th><th>Explanation</th>
  </tr>
  <tr>
    <td>
      <code>.</code>
    </td>
    <td>
      <ol>
        <li><code>"/Users/h3rald/test"</code></li>
      <ol>
    </td>
    <td>
      The symbol <code>.</code> is pushed on the stack, and it is resolved to the full path to the current folder.
    </td>
  </tr>
  <tr>
    <td>
      <code>ls</code>
    </td>
    <td>
      <ol>
        <li><code>{{fdlist}}</code></li>
      </ol>
    </td>
    <td>
      The symbol <code>ls</code> is pushed on the stack, and a list containing all files and folders in the current folder is pushed on the stack.
    </td>
  </tr>
  <tr>
    <td>
      <code>(ftype 'file ==)</code>
    </td>
    <td>
      <ol>
        <li><code>(ftype 'file ==)</code></li>
        <li><code>{{fdlist}}</code></li>
      </ol>
    </td>
    <td>
      The quotation <code>(ftype 'file ==)</code> is pushed on the stack.
    </td>
  </tr>
  <tr>
    <td>
      <code>filter</code>
    </td>
    <td>
      <ol>
        <li><code>{{flist}}</code></li>
      </ol>
    </td>
    <td>
      The symbol <code>filter</code> is pushed on the stack, and an array containing only the files present in the current folder is pushed on the stack.
    </td>
  </tr>
</table>

> %tip%
> Tip
> 
> The {{#link-module||seq#}} provides several symbols to work with quotations in a functional way.

{#link-learn||variables||Variables#}