all repos — min @ regex

A small but practical concatenative programming language.

site/contents/learn-control-flow.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
 81
 82
 83
-----
content-type: "page"
title: "Learn: Control Flow"
-----
{@ _defs_.md || 0 @}


The {#link-module||lang#} provide some symbols that can be used for the most common control flow statements. Unlike most programming languages, min does not differentiate between functions and statements -- control flow statements are just ordinary symbols that manipulate the main stack.


## Conditionals

The following symbols provide ways to implement common conditional statements:

* {#link-operator||lang||case#}
* {#link-operator||lang||if#}
* {#link-operator||lang||unless#}
* {#link-operator||lang||when#}

For example, consider the following program:

     (
       (  
         "" :type
         (("\.(md|markdown)$") ("markdown" @type))
         (("\.txt$") ("text" @type))
         (("\.min$") ("min" @type))
         (("\.html?$") ("HTML" @type))
         ((true) ("unknown" @type))
       ) case 
       "This is a $1 file." (type) % echo
     ) ^display-file-info

This program defines a symbol `display-file-info` that takes a file name and outputs a message displaying its type if known.


## Loops

The following symbols provide ways to implement common loops:

* {#link-operator||lang||foreach#}
* {#link-operator||lang||times#}
* {#link-operator||lang||while#}


For example, consider the following program:

     (
       :n
       1 :i
       1 :f
       (i n <=)
       (
         f i * @f 
         i succ @i
       ) while
       f
     ) ^factorial

This program defines a symbol `factorial` that calculates the factorial of an integer iteratively using the symbol {#link-operator||lang||while#}.

## Error handling

The following symbols provide ways to manage errors in min:

* {#link-operator||lang||format-error#}
* {#link-operator||lang||raise#}
* {#link-operator||lang||try#}

For example, consider the following program:

     . ls 
     (
       (
         (fsize) 
         (pop 0)
       ) try
     ) map 
     1 (+) reduce

This program calculates the size in bytes of all files included in the current directory. Because the {#link-operator||fs||fsize#} symbol throws an error if the argument provided is not a file (for example, if it is a directory), the `try` symbol is used to remove the error from the stack and push `0` on the stack instead.

{#link-learn||shell||Shell#}