[mercury-users] List comprehensions?

Robert Bossy bossy at ccr.jussieu.fr
Sat Apr 28 22:37:12 AEST 2001


> 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]

Of course it is possible, thanks to higher order predicates and functions:

Start = filter_map((func(N) = N * N is semidet :- N mod 3 \= 0),1 `..` 10).


The list module has a lot of useful HO predicates and funcs:
	map	to translate a list to another, element per element
	filter	to select some elements from a list
	foldl	to compute a value from the successive elements of a list
	etc.

filter_map combines the map and filter fonctionalities and could be defined as:

:- func list__filter_map(func(X) = Y, list(X)) = list(Y).
:- mode list__filter_map(func(in) = out is semidet, in) = out is det.
filter_map(_function,[]) = [].
filter_map(Function,[Hd0|Tl]) = L :-
	(
		if	Hd = Function(Hd0)
		then	L = [Hd|filter_map(Function,Tl)]
		else	L = filter_map(Function,Tl)
	).



Regards,

-- 
sig(Robert,Bossy) :-
      bossy at ccr.jussieu.fr
.
--------------------------------------------------------------------------
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