[Haskell-beginners] generating the set of all finite-valued ...

Erik Quaeghebeur equaeghe at nospammail.net
Thu Apr 23 18:16:06 EDT 2009


> On Thu, Apr 23, 2009 at 03:45:27PM +0200, Erik Quaeghebeur wrote:
>>
>> I'd like to lazily generate the set of all {-1,0,1}-valued functions on
>> {'a','b','c'}? How should I best approach this. I was thinking about
>> generalizing the power set definition

On Thu, 23 Apr 2009, Jan Jakubuv wrote:
>
> Try to start with this:
>
>    mapM (\x -> [(x,-1),(x,0),(x,1)]) ['a','b','c']

Aha. Great. Thanks, Jan. And now I realized that I don't really care about 
the domain, so I said:

Prelude> let m = mapM (\x -> [(x,-1),(x,0),(x,1)]) ['a','b','c']
Prelude> map (\x -> snd $ unzip x) m
[[-1,-1,-1],[-1,-1,0],[-1,-1,1],[-1,0,-1],[-1,0,0],[-1,0,1],[-1,1,-1],[-1,1,0],[-1,1,1],[0,-1,-1],[0,-1,0],[0,-1,1],[0,0,-1],[0,0,0],[0,0,1],[0,1,-1],[0,1,0],[0,1,1],[1,-1,-1],[1,-1,0],[1,-1,1],[1,0,-1],[1,0,0],[1,0,1],[1,1,-1],[1,1,0],[1,1,1]]

Any more direct way of doing this? (Still trying to understand how the 
mapM works... I've found it's sequence.map, but that doesn't really help.)

The next step will be to choose a good data format for what I'm trying to 
achieve. The functions (now represented as lists) would more naturally be 
represented as column matrices, e.g., from Numeric.LinearAlgebra of 
hmatrix, as I'll be needing some linear system solver. I can do the 
transition from lists to that, but I'm afraid they don't give me the 
necessary flexibility; I'd need to filter them based on (maximum/minimum) 
component values and sum them and such. Any thoughts on that?

Erik


More information about the Beginners mailing list