; prolog-types
Learning Center
Plans & pricing Sign in
Sign Out
Your Federal Quarterly Tax Payments are due April 15th Get Help Now >>



  • pg 1
									     Prolog data types

 In Prolog, list literals are
delimited by brackets with
elements separated by
commas, e.g.,
[red, yellow, green]

  The empty list is
represented as [].
  The list obtained by
adding the item Head to the
beginning of the list Tail is
represented as
   [ Head | Tail ]

  A length predicate and
other predicates are
described in the online
  Prolog has a structure, or
compound term, data type.

  Literals of this type are
syntactically identical to
relational expressions, e.g.,


  The infix operator =..
can be used to convert
structures to lists, e.g.
     X =.. [f,3,4].
will bind X to f(3,4).
  The predicates functor
and arg can be used to
access components of a
structure. For example,


will bind X to f and Y to 2.

   Here 2 is the arity of f.
-- the number of arguments
it takes. Conventionally the
notation f/2 refers to a
binary predicate f.
 Structures may be called
by the unary call (that is,
call/1) predicate.

 For example,

X=..[abs,-3,Y], call(X).

succeeds with X bound to
abs(-3,3), and Y to 3.

  Prolog has two different
string types.

  The literals of the two
type are delimited by single
quotes and double quotes

 Strings of the first type
may be used as functors
(e.g., as the first argument
of the functor predicate).
  Strings of the first type
are atoms. That it, they
satisfy the unary predicates
atom and atomic.

 Strings of the second type
are equivalent to lists of
character codes.

  Both types may appear as
the first argument of the
type conversion predicates
string_to_atom and
  For numeric types, Prolog
has infix arithmetic

    +, -, *, and /

  They are normally used in
conjunction with the infix
relation is, e.g.,

      X is 3 + 4.

which will bind X to 7.
  Prolog has infix
arithmetic predicates

<, >, =<, >=, =\=, and =:=

  Do not use = or == for
arithmetic equality.

  Prolog has a unary prefix
operator \+ for negation.
The equivalent prefix
function not is deprecated.
  The difference list
representation for lists can
be useful when frequent
appending needs to be
done, as in parsing.

  Here a list X is
represented as two lists Y
and Z such that appending
X to Z gives Y.

  In other words, X is the
difference of Y and Z.
 Conventionally a slash or
hyphen is placed between Y
and Z.

 So for example, the list

         [a, dog]

can be represented as


or as

  So if V1 and V2 are
variables, appending two
lists L1/V1 and L2/V2 gives
L1/V2 by binding V1 to L2.

  This can be done in O(1)

  The empty list can be
represented by X/X.

  An ordinary list L can be
represented as L/[].
  Prolog has many type-
checking predicates.

  See the "built-in
predicates | verify type of a
term" section of the SWI-
Prolog online manual for

  Except for the var
predicate, each of them
needs its argument to be
instantiated (i.e., not a
variable) to succeed.

To top