14.11.2012 Views

Curry: An Integrated Functional Logic Language

Curry: An Integrated Functional Logic Language

Curry: An Integrated Functional Logic Language

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

since the variables occurring in parameters of the left-hand side are only visible in the corresponding<br />

conditions and right-hand sides. The visibility of other entities can be restricted using let in<br />

expressions or where in defining equations.<br />

<strong>An</strong> expression of the form let decls in exp introduces a set of local names. The list of local<br />

declarations decls can contain function definitions as well as definitions of constants by pattern<br />

matching. The names introduced in these declarations are visible in the expression exp and the<br />

right-hand sides of the declarations in decls, i.e., the local declarations can be mutually recursive.<br />

For instance, the expression<br />

let a=3*b<br />

b=6<br />

in 4*a<br />

reduces to the value 72.<br />

Auxiliary functions which are only introduced to define another function should often not be<br />

visible outside. Therefore, such functions can be declared in a where-clause added to the righthand<br />

side of the corresponding function definition. This is done in the following definition of a fast<br />

exponentiation where the auxiliary functions even and square are only visible in the right-hand<br />

side of the rule for exp:<br />

exp b n = if n==0<br />

then 1<br />

else if even n then square (exp b (n ‘div‘ 2))<br />

else b * (exp b (n-1))<br />

where even n = n ‘mod‘ 2 == 0<br />

square n = n*n<br />

Similarly to let, where-clauses can contain mutually recursive function definitions as well as definitions<br />

of constants by pattern matching. The names declared in the where-clauses are only visible<br />

in the corresponding conditions and right-hand sides. As a further example, the following <strong>Curry</strong><br />

program implements the quicksort algorithm with a function split which splits a list into two lists<br />

containing the smaller and larger elements:<br />

split e [] = ([],[])<br />

split e (x:xs) | e>=x = (x:l,r)<br />

| e

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!