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.

A Example Programs<br />

This section contains a few example programs together with some initial expressions and their<br />

computed results.<br />

A.1 Operations on Lists<br />

Here are simple operations on lists. Note that, due to the logic programming features of <strong>Curry</strong>,<br />

append can be used to split lists. We exploit this property to define the last element of a list in a<br />

very simple way.<br />

-- Concatenation of two lists:<br />

append :: [t] -> [t] -> [t]<br />

append [] ys = ys<br />

append (x:xs) ys = x:append xs ys<br />

-- Naive reverse of all list elements:<br />

rev :: [t] -> [t]<br />

rev [] = []<br />

rev (x:xs) = append (rev xs) [x]<br />

-- Last element of a list:<br />

last :: [t] -> t<br />

last xs | append _ [x] =:= xs = x where x free<br />

Expressions and their evaluated results:<br />

append [0,1] [2,3] ❀ [0,1,2,3]<br />

append L M =:= [0,1]<br />

❀ {L=[],M=[0,1]} | {L=[0],M=[1]} | {L=[0,1],M=[]}<br />

rev [0,1,2,3] ❀ [3,2,1,0]<br />

last (append [1,2] [3,4]) ❀ 4<br />

38

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

Saved successfully!

Ooh no, something went wrong!