[Haskell-cafe] Data creation pattern?

Eugeny N Dzhurinsky bofh at redwerk.com
Thu May 13 13:40:54 EDT 2010


On Thu, May 13, 2010 at 04:43:26PM +0100, Stephen Tetley wrote:
> Hi Eugene
> 
> You don't need to supply all the arguments to a constructor at once:
> 
> makeWithOne :: String -> (String -> String -> Object)
> makeWithOne s1 = \s2 s3 -> Object s1 s2 s3
> 
> -- or even:
> -- makeWithOne s1 = Object s1
> 
> This builds a higher-order function that can be applied later to two
> Strings to finally make the Object.

Hello, Stephen!

Well, that's true, but I don't really know the order of incoming data. So for
constructor MyObject { prop1, prop2, prop3 :: String } there can be the cases
of order

prop1
prop2
prop3

prop3
prop2
prop1

prop1
prop3
prop2

...

so I may need to initialize prop1, then prop3, and finally prop2 to make
instance of MyObject. And similarly for another cases.

I don't see any easy solution for now except sort name-value pairs in some
pre-defined order, and then use something like

> props :: [(String,String)]
> result = MyObject (props' !! 0) (props' !! 1) (props' !! 2)
>     where
>         props' = map snd props

which looks ugly and involves some additional calculations for sorting.

-- 
Eugene Dzhurinsky
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20100513/efbaea6c/attachment.bin


More information about the Haskell-Cafe mailing list