Curry Examples

# Curry: Example Programs

Here is a collection of (small) Curry programs. References to larger applications can be found here. Usually, each program contains some example goals at the end in order to see its intended application.

The programs are sorted by the main language features they use so that people interested in the various features of Curry can look at these examples:

- Purely functional programs
- Purely logic programs
- Functional logic programs
- Non-deterministic functions
- Passive constraints (logic programming with coroutining)
- Residuation and concurrent programming
- Input/Output (monadic I/O)
- Arithmetic sequences and list comprehensions:
- Encapsulated search
- Modules
- Comparison with Escher
- Meta-programming
- Examples for GUI programming with library "GUI"
- Examples for functional logic parsers
- Various examples programs

### Purely functional programs:

- rev.curry: list concatenation and naive reverse
- higher.curry: some standard higher-order functions: map and foldr
- quicksort.curry: functional quicksort based on filtering the smaller and larger elements
- qsortlet.curry: quicksort based on splitting the unsorted list into a pair of lists with smaller and larger elements
- inflists.curry: functions defined on infinite lists, like fib and primes
- escher_higher.curry: implementation of module "Higher" from the Escher report

### Purely logic programs:

- colormap.curry: map coloring with classical generate-and-test
- family_rel.curry: a database for family relationships (relational style)
- family_con.curry: a database for family relationships (constraint style)

### Functional logic programs:

- nats.curry: a basic example: natural numbers are represented as S-terms and the substraction between numbers is defined by reversing the addition
- family_fun.curry: a database for family relationships (functional logic style)
- family_nd.curry: a database for family relationships (functional logic style with non-deterministic functions)
- horseman.curry: a puzzle from equational logic programming (solving questions like "How many men and horses have 8 heads and 20 feet?")
- first.curry: lazy functional logic programming with infinite lists
- last.curry: compute the last element of a list using a flexible append function (i.e., inverting list concatenation)
- member.curry: list membership defined by a single conditional rule based on a flexible append function (i.e., inverting list concatenation)
- mergesort.curry: merge sort: sorting two lists by merging the sorted first and second half of the list
- assembler.curry: a one-pass assembler: translation of a sequence of arbitrary jump-instructions into machine code where logical variables are used to resolve forward jump addresses

### Non-deterministic functions:

- nondetfunc.curry: some "classical" examples for non-deterministic functions like non-deterministic list insertion, permutation, and permutation sort
- family_nd.curry: a database for family relationships (functional logic style with non-deterministic functions)
- colormap_nd.curry: graph coloring with non-deterministic functions exploiting the demand-driven search due to lazy evaluation in Curry

### Passive constraints (logic programming with coroutining):

- colormap.curry: map coloring with test-and-generate (corresponds to logic programming with delays)
- digit.curry: small example for the use of arithmetic functions acting as passive constraints
- rigidadd.curry: small example from the POPL'97 paper for the evaluation with residuation: the addition of natural numbers acts as a passive constraint waiting for the instantiation of is arguments

### Residuation and concurrent programming:

- see also all examples for passive constraints
- infresiduate.curry: an example for a potential disadvantage of residuation [Hanus JLP'95]: with residuation, the program has an infinite search space but with narrowing, the search space is finite
- account.curry: concurrent object-oriented programming: a bank account implemented as an active object ("account server")

### Input/Output (monadic I/O):

- iodemo.curry: some demos of the monadic I/O concept
- diamond.curry: printing a regular diamond of numbers on the standard output (an adaptation from the Prolog programming contest at JICSLP'98 in Manchester)

### Arithmetic sequences and list comprehensions:

- arithseq.curry: some simple examples for the arithmetic sequence notation
- multgen.curry: a few examples for list comprehensions with multiple generators
- psort.curry: an implementation of permutation sort using list comprehensions
- default.lcurry: an implementation of Haskell-like default rules with list comprehensions

### Encapsulated search:

- allsols.curry: a few examples for the use of the library "AllSolutions"
- search.curry: a simple example for encapsulated search
- prolog.curry: a simulation of the Prolog top-level shell with different search strategies
- best.curry: a simple example for the best solution search strategy
- england.curry: a relational database with encapsulated search examples
- sportsdb.curry: module SportsDB from the Escher report showing the implementation of Escher's "forall" construct by encapsulated search

### Modules:

- ModConc.curry: a module defining list concatenation several times to be important by the example modules below
- UseConc1.curry: a module importing module `ModConc`
- UseConc2.curry: a module importing module `ModConc` qualified in order to avoid name conflicts

### Comparison with Escher:

Here are a few examples that are implementations of programs from John Lloyd's papers on Escher.

- escher_perm.curry: module "Permute" from new Escher report (computation of permutations with a concatentation and a split function)
- escher_higher.curry: implementation of module "Higher" from the Escher report
- escher_cond.curry: module "Conditional" from the Escher report (check membership in a list)
- relational.curry: module "Relational" from the Escher report (computing with relations)
- sportsdb.curry: module SportsDB from the Escher report showing the implementation of Escher's "forall" construct by encapsulated search
- england.curry: a geographical database and some queries from the Escher report

### Examples for meta-programming

Important note: The programs in this section work only with the libraries of the PAKCS environment for meta-programming (see library directory `pakcs/lib/meta` of the current PAKCS distribution).

- Imports.curry: Example for using the library `CurryStringClassifier`: extracts the names of all imported modules from a Curry source file.
- putModuleHead.curry: Example for using the library `CurryStringClassifier`: adds a standard module header to a Curry source file if it is not already there.

### Examples for the GUI library "GUI"

Important note: The programs in this section work only with the library `GUI` which is available for the PAKCS environment. A description of the ideas behind this library can be found in the PADL'00 paper. Most of the programs have a function "main" to start the GUI.

- hello.curry: a simple "Hello world" GUI
- counter.curry: a simple counter GUI
- temperature.curry: a converter from Celsius to Fahrenheit and Kelvin where the Celsius value of the temperature in Celsius is fixed by a slider
- calc.curry: a desk calculator showing the combination of GUIs with local state where the state is stored in an object
- checkbutton.curry: a simple demo for the use of checkbuttons
- config.curry: a simple demo to show the dynamic reconfiguration of widget parameters
- events.curry: a simple demo to show the handling of different events (e.g., left and right mouse buttons) inside one widget.
- inputmask.curry: a demo (simple address database) to show widgets in a tabular orientation by exploiting the `Matrix` construct for widget composition
- hilbert.curry: drawing a Hilbert curve in a canvas widget
- sierpinski.curry: drawing a Sierpinski curve in a canvas widget
- fractal.curry: drawing a fractal curve in a canvas widget
- mail.curry: a simple mail widget to write and send an email
- menu.curry: a simple counter GUI to demonstrate the use of menu buttons
- radiotraffic.curry: a simple demo for the use of radiobuttons: traffic light controller ensures exclusion in traffic lights.
- scrollbar.curry: a simple demo for the use of scrollbars
- textappend.curry: a simple GUI demonstrating the use of `appendValue` in `TextEdit` widgets
- textstyledappend.curry: a simple GUI demonstrating the use of `appendStyledValue` in `TextEdit` widgets (where text can be shown with colors and font styles)
- counter_controlled.curry: a simple counter GUI which can be controlled not only by the user but also by external processes that send messages to the GUI

### Examples for functional logic parsers

Here are simple examples for the use of the functional logic parser combinators which are defined in the parser library `Parser` and are adapted from the paper "A Functional Logic Perspective of Parsing" by R. Caballero and F.J. Lopez-Fraguas (Proc. FLOPS'99, Springer LNCS 1722, pp. 85-99, 1999).

- expr_parser.curry: a parser for arithmetic expressions over natural numbers
- palindrome.curry: a parser for palindromes over the alphabet 'a' and 'b' showing also the possibility to generate correct sentences with functional logic parsers

### Various example programs

- chords.curry: this program is able to generate appropriate chords for the accompaniment of a given melody. There is also a paper which sketches the implementation and describes the advantages of Curry to implement this application.
- daVinciTest.curry: some examples to show the use of the library `DaVinci` for the visualization of graphs

Back to Homepage of Curry