[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: List comprehensions



From: Kevin Hammond <kh@cs.glasgow.ac.uk>
Date: Fri, 25 Jan 91 11:39:13 GMT
To: haskell@cs.glasgow.ac.uk, peterson-john@cs.yale.edu
Subject: Re:  List comprehensions
Sender: haskell-request@cs.glasgow.ac.uk

> I would also encourage the addition of a new qualifier of the form 
>   pat = exp
> in the list comprehension syntax.  Although I have not seen any of the
> papers referenced, I assume this is the same as pat <- [exp].  

There are two alternatives, either the generator form (forget values which
don't match the pattern), or the definition form (error if the values don't
match the pattern).  It's a moot point which the user expects.  In terms
of standard notation, the two possibilities are:

	pat <- [exp]				-- generator
	pat <- [ptoe pat] where pat = exp	-- expression

(ptoe converts patterns to expressions).  The two forms have identical
semantics if the pattern is a variable.  If the pattern is non-variable,
the first form elides non-matching expressions, the second fails with
error (or generates an exception if we eventually have exceptions in 
Haskell...).

The only real difficulty is syntactic, especially if you want to allow
function definitions too (then you need to combine the syntax of definitions
with that of expressions, as we already do for patterns).  I think the
latter probably have many useful applications, e.g.

	g xs ys = [f x | y <- ys, x <- xs, f z = y + z]

Kevin