UP | HOME

Naming and Logic: programming basics with Wisp (WIP)

This article is a start of a text teaching programming basics with wisp. It is a work in process. The tour starts from scratch and teaches in examples with minimal explanation. It’s core is a map of the minimal functionality you need from Scheme to write meaningful programs.

Currently it only contains the map. Each entry in the map will be fleshed out as I get to it. That may take weeks, months, or years, depending on whether and when it grips me to finally type down what I already have in my analog notebook.

I’d have loved to publish it only once all is written, but I considered it more important to get the map into the open.

I hope that one day it will be interactive, using Guile on wasm (which is currently being built by the folks at Spritely).

naming-and-logic-map-of-scheme--sketch.png

PDF (drucken)

The Map of Scheme

Naming a value: define

Use define to name a value. Use . to return a value.

define small-tree-height-meters 3
define large-tree-height-meters 5
. small-tree-height-meters
$1 = 3

You should see

$1 = 3

This means: the first returned value ($1) is 3. The next time you return a value, it will be called $2.

Names can contain any letter except for space, quote, comma or parentheses. They must not be numbers.

define illegal name 1
define 'illegal-name 2
define ,illegal-name 3
define illegal)name 4
define 1113841 5
While compiling expression:
Syntax error:
unknown location: source expression failed to match any pattern in form (define illegal name 1)
While reading expression:
#<unknown port>:4:16: unexpected ")"

Use logic with numbers

= 3 5
$1 = #f

= 3 3
$1 = #t

#t means true, #f means false.

Returns the result of logic without needing a period (.).

The logic comes first. This is clear for =, but easy to misread for >.

Use logic with infix

. {3 = 5}
$1 = #f

Infix logic gives a value, so you need . to return it.

Because infix-logic gives a value, you can use it in place of a value.

For example to nest it:

. {{5 < 3} equal? #f}
$1 = #t

Or to name it as value:

define is-math-sane? {3 < 5}
. is-math-sane?
$1 = #t

By convention, names that have the value true or false have the suffix ?.

Use logic with names

define small-tree-h/m 3
define large-tree-h/m 5
. {small-tree-h/m < large-tree-h/m}
$1 = #t

Logic with true and false

and #t #t
$1 = #t

and #f #t #t
$1 = #f

If any logical value passed to and is false, further values are not checked.

or #f #f #t
$1 = #t

Or checks whether any value is #t.

Name the result of logic with indentation

define birch-h/m 3
define chestnut-h/m 5
define same-héight?
  = birch-h/m chestnut-h/m
define smaller?
  . {birch-h/m < chestnut-h/m}

The infix gives a value, so it needs the ..

Name logic with define :

define : same-heigth? tree-height-a tree-height-b
  = tree-height-a tree-height-b
same-height? 3 3
ice-9/boot-9.scm:1676:22: In procedure raise-exception:
Unbound variable: same-height?

Entering a new prompt.  Type `,bt' for a backtrace or `,q' to continue.


By convention, logic that returns true or false has the suffix ?.

You can now use your named logic like all other logic. Even with infix.

define : same-height? tree-height-a tree-height-b
  = tree-height-a tree-height-b
. {3 same-height? 3}
$1 = #t

Name a name in define with .

define small-tree-height-meters 3
define height
  . small-tree-height-meters
. height
$1 = 3

. returns the value of its line.

Return the value of logic with .

define : larger-than-4? size
  . {size > 4}
. larger-than-4?
$1 = #<procedure larger-than-4? (size)>

The value of logic defined with define : is a procedure. You can see the arguments in the output: If you call it with too few or too many arguments, you get warnings.

There are other kinds of logic: syntax rules and reader-macros. We will cover syntax rules later. New reader macros are rarely needed; using {...} for infix math is a reader macro.

To Be Continued …

ArneBab 2023-10-28 Sa 00:00 - Impressum - GPLv3 or later (code), cc by-sa (rest)