W Wrapl, The Programming Language

Chapter 2

Converting Types

The basic types in Wrapl are provided by the modules in Std.*. To convert from one type to another, one uses the conversion operator @.

--> IMP Std.String; NIL --> 10 @ String.T; "10" --> IMP Std.Integer; NIL --> "123" @ Integer.T; 123

Variables

All variables in Wrapl must be declared, using VAR ident;. To assign a value to a variable, use ident <- value;, before assigned a value, variables have the value NIL. A variable can be assigned a value when declared, using VAR ident <- value;, this will have the same effect as VAR ident; ident <- value;. Several variables can be declared in the same VAR declaration by separating them with commas.

--> VAR x; NIL --> x <- 100; 100 --> x + 20; 120 --> VAR y <- 15; 15 --> x - y; 85 --> z; console(6): Error: identifier z not declared

The assignment operator <- returns the assigned value, so that several variables can be assigned the same value in one expression.

--> VAR a, b, c; NIL --> a <- b <- c <- 21; 21 --> a; 21 --> b; 21 --> c; 21

Within the interactive interpreter, the predeclared variable _ holds the result of the last evaluated expression.

--> 2 ^ 5; 32 --> _ / 2; 16

Numbers

Integers

Wrapl provides support for integers of arbitrary precision (limited only by available memory).

--> 2 + 2; 4 --> (100 - 6 * 7) / 2; 29 --> 2 ^ 100; 1267650600228229401496703205376

Integers by default are written in decimal (base 10), but can be written in other bases by preceding the number with the base, in decimal. Integers are normally converted to strings in decimal, but other bases can be used by passing the base as an additional parameter to @.

--> 2_10101; 21 --> 16_1A2F; 6703 --> IMP Std.String; NIL --> 6703 @ String.T; "6703" --> 6703 @ (String.T, 16); "1A2F"

Rational Numbers

Wrapl provides rational numbers of arbitrary precision. Rationals can be written as numerator/denominator, with no space around the /. Rationals are returned by any division involving integers where there is a remainder. To get the integer part of a division involving integers, use :div or :floor.

--> 1/2; 1/2 --> (17 - 4) / 3; 13/3 --> 13/3:num; 13 --> 13/3:den; 3 --> 13:div(3); 4 --> 13/3:floor; 4 --> 13/3:ceil; 5

Real Numbers

Real numbers are stored in Wrapl using the standard (IEEE 754, double precision) floating point representation. It is possible to convert between real numbers and rationals. Converting a rational to a real will usually result in a loss of precision.

--> 3.4 * 7.6; 25.84 --> IMP Std.Rational, Std.Real; NIL --> 19/7 @ Real.T; 2.7142857142857139685 --> 2.7142857142857139685 @ Rational.T; 19/7 --> IMP Math.Constant USE Pi; NIL --> Pi; 3.141592653589793116 --> (Pi / 2):sin; 1

Strings

Strings in Wrapl can be entered in one of three ways. More information can be found here.

--> "This is a string"; "This is a string" --> "A string with \"escape sequences\"\n"; "A string with \"escape sequences\"\n" --> >>BLAH ... Block strings can span\ ... several lines, and do not parse ... \tescape sequences ... BLAH; "Block strings can span several lines, and do not parse\n\\tescape sequences\n" --> 'Complex strings can contain {"embe" + "dded"} expressions, escape sequences ... and span several lines'; "Complex strings can contain embedded expressions, escape sequences\nand span several lines" --> VAR x <- 10; 10 --> 'x = {x}'; "x = 10"

Strings in Wrapl are immutable, new strings can be constructed by concatenation (using +), taking substrings (using []) and repetition (using *). String indices begin at 1 for the first character through to string:length for the last. In addition, negative indices start from the end of the string, -1 being the last character. Substrings are inclusive of the first index and exclusive of the last.

--> VAR s <- "Hello " + "world!"; "Hello world!" --> s[5]; "o" --> s:length; 12 --> s[12]; "!" --> s[-1]; "!" --> s[7, -1]; "world" --> "ab" * 5; "ababababab"

Navigate