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


Groups > comp.lang.haskell > #564

Nested "Maybe"

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.haskell
Subject Nested "Maybe"
Date 2021-04-09 22:13 +0100
Organization A noiseless patient Spider
Message-ID <878s5rtb4g.fsf@bsb.me.uk> (permalink)

Show all headers | View raw


This group is looking quiet, but you never know...

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) but, equally obviously, I can't
write a function like this

  f a | a < 0     = Nothing
      | a > 10000 = Roughly (sqrt a)
      | otherwise = Just (a / 2)

without type errors.  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))

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

-- 
Ben.

Back to comp.lang.haskell | Previous | NextNext 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