Language Guide: Page 5

A crash course in the particular workings of microscheme

Microscheme Libraries

Microscheme supports the (include …) primitive, which effectively loads the whole contents of another file into the program. This allows commonly used program segments to be saved in 'libraries' that can be included in any other program. Typically, libraries contain definitions, but do not perform any input or output, so including them simply makes a set of procedures and data structures available to the program. Some useful libraries are included with microscheme, and more will become available as the project matures:

libraries/io.ms provides digital Input/Output functionality. This allows you to work with the Arduino's digital I/O pins, using the indices given to them on the arduino board. It provides the procedures:

  • (set-ddr N X) to set the DDR (data-direction-register) for a pin. N is the pin number. X is #f for ‘input’ and #t for ‘output’.
  • (get-ddr N) returns a boolean representing the DDR value for pin N. #t means ‘output’.
  • (set-pin N Y) sets the value (high or low) for pin N.
  • (set-pin N Y) gets the value (high or low) of pin N.

libraries/list.ms provides various functions for working with lists, which are linear data structures built using pairs and null. Procedures provided include:

  • (list? X) returns true if and only if X is a list.
  • (reverse X) if X is a list, returns a new list which is the reverse of it.
  • (map P X) returns a list formed by performing procedure P on every element of list X.
  • foldr, foldl, for-each, all various common higher-order list procedures.
  • (vector->list V) returns a list whose elements are identical to those of vector V.
NB: the primitive (list …) for building lists is built-in, and implemented efficiently by the compiler.

libraries/long.ms provides an implementation for 8-digit unsigned integers:

  • (long hi lo) forms a long where hi represents the high four digits and lo represents the low four digits of the number. The number 994020 is produced by (long 99 4020).
  • (hi X) and (lo X) extract the high and low parts of a long.
  • (long? X) returns true if X is a valid long. Warning: any pair of numbers will satisfy this.
  • l+ l- l* l/ standard arithmetic operators. (NB: l* and l/ are slow, software-based implementations.
  • l++ l-- l** are in-place versions of l+ l- and l*. i.e. (l++ lX lY) is equivalent to (set! lX (l+ lX lY)), but allocates no new memory. You should use these operators wherever possible
  • l= l< l> l<= l>= standard numeric comparators.

libraries/fixedpoint.ms provides an implementation for 5+5 digit unsigned fixed-point reals:

NB: including the xtypes library has the same effect as including long and fixedpoint individually, but saves memory by taking advantage of the overlap between their functions.