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


Groups > comp.lang.haskell > #567

Re: Nested "Maybe"

From Mark Carroll <mtbc@bcs.org>
Newsgroups comp.lang.haskell
Subject Re: Nested "Maybe"
Date 2021-04-09 17:33 -0400
Organization none
Message-ID <874kgfywg1.fsf@ixod.org> (permalink)
References <878s5rtb4g.fsf@bsb.me.uk>

Show all headers | View raw


On 405 Mar 2020, Ben Bacarisse wrote:

> Recently I wanted a type that adds another alternative to Maybe,
> specifically an approximate result, so I wrote (somewhat without
> thinking)
>
>   data Approximate a = Roughly a | Maybe a
>
> Haskell does not complain about the type (after all, it just looks like
> I'm defining a constructor called Maybe)

Yes, I think that's exactly what you're doing, unconnected with the type
Maybe.

(snip)
> I can nest the Maybe in a new type:
>
>   data Approximate a = Roughly a | Exactly (Maybe a)
>
>   f a | a < 0     = Exactly Nothing
>       | a > 10000 = Roughly (sqrt a)
>       | otherwise = Exactly (Just (a / 2))

That's what I would have done, depending on why I wanted to use Maybe at
all. E.g., if you don't need much of what Maybe does, you could ignore
it and define your own three-option type and reimplement the Maybe-like
stuff you do need. Or you may find you get much of what you need if you
wrap the other way, e.g., leave it as a,

  data Approximate a = Roughly a | Exactly a

and use Maybe (Approximate a).

> but I can't help wondering if I'm missing a neater way to do this.

I'm not aware of one but I know only "pedestrian" Haskell so somebody,
perhaps more familiar with esoteric extensions, may be able to show us
some magic.

-- Mark

Back to comp.lang.haskell | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Nested "Maybe" Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-04-09 22:13 +0100
  Re: Nested "Maybe" Paul Rubin <no.email@nospam.invalid> - 2021-04-09 14:24 -0700
    Re: Nested "Maybe" Paul Rubin <no.email@nospam.invalid> - 2021-04-09 14:27 -0700
  Re: Nested "Maybe" Mark Carroll <mtbc@bcs.org> - 2021-04-09 17:33 -0400

csiph-web