This post will introduce the atomic data types in Motto.
Numbers
Various types of numbers supported by Motto are – complex, real, rational and integer.
> 3+4i ;complex
> 3.14 ;real
> #e1e10 ;real
> 6/10 ;rational
> 300 ;integer
There are predicates that can recognize the accurate type of a number:
> ; Here only the result of the last operation is shown.
; The actual REPL will print all values on the stack.
> -2.5+0.0i is-complex
#t
> -2.5+0.0i is-real
#f
> 6/10 is-rational
#t
> 3 is-integer
#t
A number can have these radix prefixes: #b (binary), #o (octal),
#d (decimal), and #x (hexadecimal). With no radix prefix, a number is
assumed to be decimal.
Some operations with numbers:
> 34 number-to-string is-string
#t
> ;convert to base 2 representation.
> 2 34 number-to-string-r
"100010"
> ;convert a string in base 16 representation to an integer.
> 16 "#xFFE" string-to-number-r
4094
> 10 factorial
3628800
> 100 sqrt
10
> ;generate a random number between 1 and 10.
> 10 random-integer
8
> ;generate a random number between 0 and 1.
> random-real
.8574025375628211
A division by zero will return the special symbol +nan.0 which means “Not A Number”.
Any arithmetic operation with this value will return +nan.0. The symbols +inf.0 and
-inf.0 represent positive and negative infinities.
Boolean
The literals #t (true) and #f (false) are used to represent Boolean values.
Procedures that return Boolean values are known as predicates.
There are built-in predicates for comparing two objects on the stack:
> 4 3 gt ;greater than.
#t
> 4 3 lteq ;less than or equals.
#f
> 10 10.00001 eq ;equality
#f
> red red eq ;eq works for symbols too.
#t
> "red" "red" eq ;but not for strings.
#f
> "red" "red" string-eq
#t
The if procedure can be used to conditionally execute code based on a Boolean value
at the top of the stack. For example, the following procedure will print stop if the
signal is red:
> [start stop rot red eq if .s .c] manage-traffic @
> red manage-traffic
stop
> green manage-traffic
start
When manage-traffic is invoked the first time, there are four values on the stack:
The call to rot will transform the stack like this:
Now eq will leave #t on the stack:
As the top value is #t, if will leave stop on the stack and drop start.
If the top value is #f, it will drop the next value (stop) instead.
(I will discuss control structures in detail in a future post.).
Character
A character literal is preceded by #\. Characters are Unicode encoded.
As Motto internally uses the Scheme read procedure to parse its input, it can recognize
special character literals
like #\space and #\newline.
> #\a #\a char-eq
#t
> #\a #\A char-eq
#f
> ;compare by ignoring case.
> #\a #\A char-ci-eq
#t
> ;get the UNICODE encoding of the character.
> #\c char-to-integer
99
Symbol
A symbol is a unique identifier. It can be used as constants, variable names etc.
The following code block declares a list of symbols to represent the months in a year.
> [jan feb mar apr may jun jul aug sep oct nov dec] months @
> months
jan feb mar apr may jun jul aug sep oct nov dec
It is easy to check the stack for a particular symbol using the contains predicate:
> .c
> [months 12 at 13 remove-at contains no yes rot if println .c] is-month @
> [display newline] println @
> jan is-month
yes
> aug is-month
yes
> blah is-month
no
Symbols with a colon at the end are called “keywords”. (A colon by itself is not a keyword, it is a symbol.).
Keywords are similar to symbols but are self evaluating and distinct from the symbol data type.
They can be used for specifying key-value pairs and to pass named arguments to procedures:
> name: sam age: 34
> name: get println ; get helps us to retrive a value mapped to a key.
sam
> age: get println
34
> .c
The following procedure that computes the distance between two points on a
Cartesian Plane accept keyword arguments, so the order in which the arguments are
passed becomes irrelevant:
> [x2: get x1: get - 2 expt y2: get y1: get - 2 expt + sqrt] distance @
> x1: 10 y1: 20 x2: 34 y2: 21 distance println .c
24.020824298928627
> x1: 10 y2: 21 x2: 34 y1: 20 distance println .c
24.020824298928627
The next post will explore the composite data types in Motto.