# 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:

• 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

### 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:

• 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")

• 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

### Functional logic programming with constraints:

Important note: The programs in this section work only with implementations of Curry that support real arithmetic and finite domain constraints, see, for instance, PAKCS.
• mortgage.curry: a classical example for constraint logic programming over reals: define a mortgage relationship between the principal, the life of the loan in months, the interest rate, the monthly repayment, and the outstanding balance. Due to the constraint programming features, one can solve various problems with this simple definition.
• circuit.curry: a further classical example for constraint logic programming over reals: analysis of electrical circuits
• queens.curry: a classical example for constraint logic programming over finite domains: solving the n-queens problem
• smm.curry: a further classical example for constraint logic programming over finite domains: solving the "send+more=money" puzzle
• sudoku.curry: solving Su Doku puzzles in Curry with finite domain constraints. This examples shows the use of higher-order functions to set up a set of constraints in a compact way.
• magicseries.curry: an example for constraint logic programming over finite domains where lazy evaluation is exploited: the computation of magic series

### Distributed functional logic programming

Important note: Most of the programs in this section work only with implementations of Curry that support distributed programming with port constraints as described in the PPDP'99 paper.
• accountport.curry: the same program as account.curry (a bank account implemented as a concurrent object) but with the use of port constraints
• counter.curry: a simple counter which can be incremented and queried from any machine connected to the Internet
• nameserver.curry: a name server storing an association between names and numbers which can be queried in a simple manner from any machine connected to the Internet
• talk.curry: a simple program implementing the kernel of the Unix "talk" to enable a conversation between two partners on different machines connected to the Internet
• sema.curry: an implementation of semaphores in Curry
• philo.curry: an implementation of "dining philosophers" problem using semaphores implemented via ports
• httpget.curry: a simple example showing the connection to Unix sockets (not implemented with ports but with more low level Socket library)
• addserver.curry: an example showing the implementation of a simple client/server application (the server provides a service to add two numbers). The implementation does not use ports but the more low level Socket library.
• addnamedserver.curry: an example showing the implementation of a simple client/server application (the server provides a service to add two numbers). The implementation does Unix sockets with symbolic names based on the library NamedSocket.
• addtimeoutserver.curry: an example showing the implementation of a simple client/server application (the server provides a service to add two numbers). The implementation uses the Socket library and shows the use of time limits for socket connections on the server side.

### 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 the deprecated GUI library "Tk"

Important note: The programs in this section work only with the GUI library Tk which is available for the PAKCS environment. This library will no longer be supported in the future since a similar but improved library GUI (see examples) is available. 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
• inputmask.curry: a demo (simple address database) to show widgets in a tabular orientation by exploiting the TkMatrix 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
• mailsearch.curry: the same as the previous mail GUI but with a "search" button to search an email address by querying an address server
• 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
• 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
Michael Hanus