ninetynine-1.3.0: Ninety-Nine Haskell Problems
CopyrightCopyright (C) 2023 Yoo Chung
LicenseGPL-3.0-or-later
Maintainerdev@chungyc.org
Safe HaskellSafe-Inferred
LanguageGHC2021

Problems.P76

Description

Part of Ninety-Nine Haskell Problems. Some solutions are in Solutions.P76.

Synopsis

Documentation

eitherGoldbach :: String -> Either String (Integer, (Integer, Integer)) Source #

In Problems.P75, maybeGoldbach returned Nothing when there is an error. However, this revealed nothing about why there is an error.

Either is a data type which can hold either of two data types, which can be used to store either an error or a correct value when there is no error. By convention when Either is used this way, the Left constructor is used for errors and the Right constructor is used for correct values. Either is also a monad.

Rewrite maybeGoldbach to return an Either value, using one of the following strings when there is an error with their obvious meanings:

  • "not a number"
  • "not greater than 2"
  • "not an even number"

Examples

>>> eitherGoldbach "104"
Right (104,(3,101))
>>> eitherGoldbach "this is not a number"
Left "not a number"
>>> eitherGoldbach "2"
Left "not greater than 2"
>>> eitherGoldbach "101"
Left "not an even number"