Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.functional > #102

Re: Converting list comprehensions to Standard ML

From Brian Adkins <lojicdotcom@gmail.com>
Newsgroups comp.lang.functional
Subject Re: Converting list comprehensions to Standard ML
Date 2011-05-22 12:56 -0400
Organization A noiseless patient Spider
Message-ID <m2lixyk728.fsf@gmail.com> (permalink)
References <m21uzrhezi.fsf@gmail.com>

Show all headers | View raw


Brian Adkins <lojicdotcom@gmail.com> 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/

Back to comp.lang.functional | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Converting list comprehensions to Standard ML Brian Adkins <lojicdotcom@gmail.com> - 2011-05-21 18:21 -0400
  Re: Converting list comprehensions to Standard ML Brian Adkins <lojicdotcom@gmail.com> - 2011-05-22 12:56 -0400
    Re: Converting list comprehensions to Standard ML torbenm@diku.dk (Torben Ægidius Mogensen) - 2011-05-23 10:46 +0200
    Re: Converting list comprehensions to Standard ML Thomas Thiel <thomas.thiel13@freenet.de> - 2011-05-28 23:31 +0200
      Re: Converting list comprehensions to Standard ML Brian Adkins <lojicdotcom@gmail.com> - 2011-06-04 19:54 -0400
  Re: Converting list comprehensions to Standard ML torbenm@diku.dk (Torben Ægidius Mogensen) - 2011-05-23 14:33 +0200
    Re: Converting list comprehensions to Standard ML torbenm@diku.dk (Torben Ægidius Mogensen) - 2011-05-23 17:48 +0200
      Re: Converting list comprehensions to Standard ML Brian Adkins <lojicdotcom@gmail.com> - 2011-05-25 17:08 -0400

csiph-web