Professional Documents
Culture Documents
Data Types
CHARACTER
character
COMPLEX
single precision complex number
DOUBLE PRECISION
double precision floating point number
INTEGER
integer
LOGICAL
boolean (true or false)
REAL
single precision floating point number
Scheme Data types
Booleans
(true) or
#f
Numbers are self-evaluating, and are entered as expected. Scheme also supports
complex numbers as for instance
0+4i
. Just typing
4i
.
Symbols
Symbols are sequences of characters, different from strings because they are not
self-evaluating, and they can't contain white-space either. They are just input by
typing them really, for instance
(add one two)
Scalar variables are used to store single values - for example, a number or a string.
They are preceded by the dollar sign ($).
Arrays (lists)
Arrays, often called lists, can store a list of many scalar values at once. They are
preceded by the at sign (@)
Associative arrays (hashes)
The final data type we're going to cover here is the associative array, often called
ahash. Associative arrays are similar to normal arrays, with one important difference.
Instead of using numbers to index each element in an array, you can use more
meaningful strings.
ABSTRACT
This module implements a class to be used from interfaces to Prolog systems (SWI-
Prolog, XSB, etc.) to transform complex Perl data types to Prolog types.
METHODS
Language::Prolog::Types::Converter->new()>
EXPORT
Pattern Matching
You should note that in the constructor declarations I didn't give the arguments
names, but rather simply specified their types. The reasoning behind this is clear as
we see pattern matching in action.
It's important to note that this kind of pattern matching is also how Haskell's case
expression works.
Robustness
Of course, this isn't very robust. There's no definition of lastName for Name1 or
NoName, and firstName isn't defined for NoName. Obviously, this is because those
forms of Name don't have the data we're trying to extra.
Polymorphism
Let's take a simple example. We want a function which will converta Name into a
suitable String for output. In Haskell we normally use the show function to do just
this. Getting show (and functions which rely on it) to accept a Name is a matter of
polymorphism.
Deriving
Similarly, we can check for equality between names by making Name an instance of
the Eq class. This class is interesting because it declares two functions: the ==
and /= operators, but it provides a default definition for the /= operator.
Of course, nothing limits us to classes included in the standard library. Let's define a
new data type which incorporates a Name. We then have two new functions to get
the first and last names.