[Haskell-beginners] elegant handling of maybe and non-maybe values

Kim-Ee Yeoh ky3 at atamo.com
Fri Sep 21 13:40:40 CEST 2012


On Fri, Sep 21, 2012 at 8:25 AM, Christopher Howard <
christopher.howard at frigidcode.com> wrote:

> But this seems rather clunky, especially if I want to attach the values in
> the new list to variable names afterwards.


Agreed. Haskell is not Lisp, so there's no attachment (TH notwithstanding)
to variable names ['a'..'z'] if that's what you're thinking.

Could you provide the concrete scenario of what you're trying to achieve?


-- Kim-Ee


On Fri, Sep 21, 2012 at 8:25 AM, Christopher Howard <
christopher.howard at frigidcode.com> wrote:

> I have a code situation similar to this simplified one: Let's say that
> I've got a value v that is a Maybe type, and values x, y, and z that are
> not. x, y, and z are dependent on the value inside v. So I must provide
> default values for x, y, and z. An additional complication is that every
> calculation returns a new (possibly different) value for v. So, I could
> do something like this:
>
> code:
> --------
> let (v', x) = case v of
>                 Nothing -> (Nothing, defaultValueOfX)
>                 Just vValue -> f vValue in
> let (v'', y) = case v' of
>                  Nothing -> (Nothing, defaultValueOfY)
>                  Just vValue -> g vValue in
> let (v''', z) = case v'' of
>                   Nothing -> (Nothing, defaultValueOfZ)
>                   Just vValue -> h vValue in
> --------
>
> (f, g, h represent the calculations I am performing.) In the end, I need
> the values of x, y, and z, and the last v value.
>
> Obviously, the above solution is not very elegant, especially if I add a
> few more variables. My other thought was some kind of fold operation,
> where I store the functions (calculations) and the default values in a
> list, and then fold over the list, applying subsequent values of v or
> returning default values, as appropriate. But this seems rather clunky,
> especially if I want to attach the values in the new list to variable
> names afterwards.
>
> Is there a better approach?
>
> --
> frigidcode.com
> indicium.us
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20120921/8a8cf6d6/attachment.htm>


More information about the Beginners mailing list