next up previous contents index
Next: Querying the Internal Database Up: Built in functions Previous: Currying   Contents   Index

Functional programming

let(Assignments Body)

  let([X=1 Y=2] writeln(X+Y))

Prolog variable assignment is used. assignments are local to the whole enclosing function call or function definition, not just the let function (this behavior may change)

map(Func List)

applies Func to every member of List and returns the resulting list

Func can be the name of a 1-argument function

  map(writeln [a b c d])
  \endP\{verbatim}
  
  Or a lambda function (see below)
  \begin{verbatim}
  map(lambda([X] X+1) [1 2 3 4]) => [2 3 4 5]
  \endP\{verbatim}
\func{lambda(Args Body)}  lambda function. evaluates to itself. See example above

  Syntactic sugar: for a single argument function, you can also say
  \func{\\Arg -> Body}

  \begin{verbatim}
  map(\X->X+1 [1 2 3]) => [2 3 4]

apply(Func Args)

function application. calls Func(Args)

  foo(X Y) := (X*Y)/2
  apply(foo [4 3]) => 6

Functions can be curried:

  foo(X Y) := (X*Y)/2
  apply(foo [4]) => lambda([Y] (4*Y)/2)
  apply(apply(foo [4]) [3]) => 6

#X

delay function. returns a reference to a function call

  #foo(1+2) => foo(1+2)
eval(X)

compiles an atom and evaluates it

  eval('1+2') => 3


chris mungall 2006-02-09