[mercury-users] List comprehensions?
Richard A. O'Keefe
ok at atlas.otago.ac.nz
Tue May 1 12:35:51 AEST 2001
Terrence Brannon <princepawn at earthlink.net> wrote:
The list comprehensions in the functional programming language Clean
are a very compact and powerful way to express list manipulation. For
example the square of all integers from 1 to 10 that are not dividable
by three are computed by the program:
Start = [n*n \\ n <- [1..10] | n rem 3 <> 0]
Is this possible in Mercury? I would think that any deductive
capabilities in a purely functional language would also be available
in a functional-logic hybrid.
List comprehensions are available in many functional languages, including
Miranda, Haskell, Clean, and Erlang. Clean has some nice unique features:
- you can generate list elements pat<-list OR array elements pat<-:array
- generators can be run side-by-side gen1&gen2&... as well as nested.
The Haskell language reference explains list comprehension quite clearly
by translating it to calls to mapping and filtering functions. (Some
functional languages, such as ML, do not include list comprehension syntax,
so the transformations are useful to know.)
The Prolog equivalent of list comprehension is bagof/3 or setof/3.
For example,
bagof(NSq, (between(1, 10, N), N mod 3 =\= 0, NSq is N*N), Start)
[I couldn't install the binary release of Mercury 0.10 for Sparc/Solaris
because I only had about 211 MB free on my free-est disc, and that wasn't
enough. Otherwise I'd give a Mercury example.]
Mercury has some all-solutions predicates, so Mercury can do it the Prolog
way. Mercury also has higher-order predicates and functions, so it can do
it the ML way.
--------------------------------------------------------------------------
mercury-users mailing list
post: mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe: Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------
More information about the users
mailing list