Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: Brian Adkins Newsgroups: comp.lang.functional Subject: Re: Converting list comprehensions to Standard ML Date: Sun, 22 May 2011 12:56:31 -0400 Organization: A noiseless patient Spider Lines: 34 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx04.eternal-september.org; posting-host="1hRJk2m14aRWSp6vwasOlQ"; logging-data="27563"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+xxBYNfSJ9qfTZ54kqhDGv" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (darwin) Cancel-Lock: sha1:ASYImMANrwAFa3UWF9mk1JmCBCA= sha1:+8NL4H1UDQMjFIwMjhCxhQKnJvA= Xref: x330-a1.tempe.blueboxinc.net comp.lang.functional:102 Brian Adkins writes: > I've been translating a simple Haskell program I wrote to Standard ML, > and I just bumped up agains this list comprehension: > > [ (r,c) | r <- [0..4], c <- [0..r], predicate (r,c) ] > > I don't think I appreciated the brevity of list comprehensions that much > until I attempted to convert this :) > > What would be the most idiomatic way to implement this in Standard ML? > The fact that the second generator depends on the first complicates > things somewhat. This is my first draft (with a little help from "ML for the Working Programmer" by Paulsen): (List.filter (fn (r,c) => predicate (r,c)) (foldr (fn (r,l) => foldr (fn (c,l) => (r,c)::l) l (upto(0,r))) [] (upto(0,4)))) I'm hoping there's a much better way though, because this seems *much* less clear than the list comprehension. By the way, is there a way to not have to write List.filter by using an include or similar statement? Thanks, Brian -- Brian Adkins http://lojic.com/