Up to Main Index                            Up to Journal for August, 2023

                     JOURNAL FOR TUESDAY 8TH AUGUST, 2023
______________________________________________________________________________

SUBJECT: If…
   DATE: Tue  8 Aug 20:35:46 BST 2023

Since Sunday’s release of Mere v0.0.4 I’ve been focusing my attention on ‘if’
statements. At the moment the ‘if’ statement is very basic:

  if condition; expression/statement

When the condition is true then the following expression or statement is then
executed. This has served Mere well. However, I think now is a good time to
introduce a more expressive ‘if’. Ironically, this change will break a lot of
Mere code. One of my main reasons for creating Mere was to have a language
that others didn’t keep breaking for me all the time :( Still, alpha quality…

I’ve been experimenting with the following structure for ‘if’ statements:


    if condition
      code block
    elseif condition    // optional elseif
      code block
    : :                 // as many elseif as you need…
    : :
    else                // optional else
      code block
    endif


As a rule I don’t clock watch and usually only vaguely know what time it is. I
even wrote many versions of a fuzzy clock. Here is a working[1] Mere version:


    >cat fuzzy-clock.mr
    hours = []string "twelve" "one"   "two"   "three" "four" "five",
                     "six"    "seven" "eight" "nine"  "ten"  "eleven"

    now = time
    hour, minute >< now\hour, now\minute

    if minute > 37; hour++; endif

    if 0 <= minute && minute <= 1
      text = "%s o'clock"
    elseif 2 <= minute && minute <= 7
      text = "Just gone %s o'clock"
    elseif 8 <= minute && minute <= 13
      text = "Nearly quarter past %s"
    elseif 14 <= minute && minute <= 16
      text = "Quarter past %s"
    elseif 17 <= minute && minute <= 22
      text = "Just gone quarter past %s"
    elseif 23 <= minute && minute <= 28
      text = "Nearly half past %s"
    elseif 29 <= minute && minute <= 31
      text = "Half past %s"
    elseif 32 <= minute && minute <= 37
      text = "Just gone half past %s"
    elseif 38 <= minute && minute <= 43
      text = "Nearly quarter to %s"
    elseif 44 <= minute && minute <= 46
      text = "Quarter to %s"
    elseif 47 <= minute && minute <= 52
      text = "Just gone quarter to %s"
    elseif 53 <= minute && minute <= 58
      text = "Nearly %s o'clock"
    else
      text = "%s o'clock"
    endif

    println
    printf text+"\n", hours[hour % 12]
    println

    >mere fuzzy-clock.mr
    Just gone half past eight
    >date
    Tue  8 Aug 20:34:02 BST 2023
    >


It’s all working, as are complex nested ‘if’ statements. But, I do really miss
the ‘if’ short form :(


    if minute > 37; hour++          // old short form
    if minute > 37; hour++; endif   // new short form
    if minute > 37                  // new expanded form
      hour++
    endif


I’m thinking of adding a ‘when’ statement to replace the ‘if’ short form:


    when minute > 37; hour++


It’s short and reads well, I quite like it. I’m just conscious of the fact I’m
taking away another useful variable name. The short ‘if’ could be kept as-is
by adding a ‘then’ keyword after the ‘if’ and ‘elseif’ conditionals:


    if condition then
      code block
    elseif condition then   // optional elseif
      code block
    : :                     // as many elseif as you need…
    : :
    else                    // optional else
      code block
    endif


Many languages take this approach and it would avoid breakage in existing Mere
code. However, I take one look at that and think “Nope… hate it…” :(

The question mark could be used. In languages that support it, the question
mark is often used for the ternary conditional operator. For example:


    condition ? <when true> : <when false>  // typical ternary conditional

    condition ? <when true>                 // possible sort if form?


There are some other decisions I need to make. Do we want ‘elseif’ and ‘endif’
or would shorter ‘elif’ and ‘fi’ be more desirable? Comparing this to other
languages:


       elseif  Dylan, Eiffel, MatLab, Modula 2, Oz, PHP, Tcl, VimScript
       endif   PHP, VimScript

       elif    Bourne Shell, Maple, Python
       fi      Beta, Bourne Shell, Maple


If you enjoy these topics see also “Conditional”[2] and “Dangling else”[3].

At the moment ‘elseif’ and ‘endif’ work. But, I’m leaning more towards:


    when condition; expression / statement

    if condition
      code block
    elif condition      // optional elif
      code block
    : :                 // as many elif as you need…
    : :
    else                // optional else
      code block
    fi


On a different topic, I’m thinking of dropping subroutines now that functions
are working. Subroutines are another feature that got Mere to where it is but
have outlived their usefulness :(

So, if you fancy yourself a bit of a programming language designer, or just
have strong preferences, email me your thoughts: diddymus@wolfmud.org

I did notice Go 1.21.0 was released a little earlier today. What fun! I
already know I’ll have to fix Mere ICE due to incompatible WASM changes :(

--
Diddymus

  [1] Only works with my development version of Mere…

  [2] Wikipedia, Conditional (computer programming):
        https://en.wikipedia.org/wiki/Conditional_(computer_programming)

  [3] Wikipedia, Dangling else:
        https://en.wikipedia.org/wiki/Dangling_else


  Up to Main Index                            Up to Journal for August, 2023