[Haskell-cafe] Is there a null statement that does nothing?
nowgate at yahoo.com
Wed Oct 21 21:27:56 EDT 2009
Those nested IF/THEN/ELSEs are real killers. I kind of use them to sort out my thoughts, then end up with a nested mess. The restructuring to WHENs went smoothly and looks a lot cleaner besides.
--- On Wed, 10/21/09, Dan Weston <westondan at imageworks.com> wrote:
From: Dan Weston <westondan at imageworks.com>
Subject: Re: [Haskell-cafe] Is there a null statement that does nothing?
To: "michael rice" <nowgate at yahoo.com>
Cc: "Tim Wawrzynczak" <inforichland at gmail.com>, "haskell-cafe at haskell.org" <haskell-cafe at haskell.org>
Date: Wednesday, October 21, 2009, 9:16 PM
If you have a long if/else if/else chain, you might consider a trivial case statement with guards. Whether you think this is attractive is a matter of taste, but it has the fall-through semantics you want and ghc optimizes the _ pattern matching away:
f x = case () of
_| x == 2 -> 22
_| x == 4 -> 44
_| x == 7 -> 77
_| otherwise -> 55
> f 4
> f 9
michael rice wrote:
> Thanks guys,
> I understand what you're telling me, but have some nested IFs and just want to fall through on one of the ELSES but then I end up with two ELSES in a row and nothing between them. Oh, well, on to restructuring.
> --- On *Wed, 10/21/09, Tim Wawrzynczak /<inforichland at gmail.com>/* wrote:
> From: Tim Wawrzynczak <inforichland at gmail.com>
> Subject: Re: [Haskell-cafe] Is there a null statement that does nothing?
> To: "michael rice" <nowgate at yahoo.com>
> Cc: haskell-cafe at haskell.org
> Date: Wednesday, October 21, 2009, 8:49 PM
> Yes, an if statement must have both 'then' and 'else' branches. As
> an example, what if you had
> let a = if b == 2 then True else False
> and you were missing an else branch? What would 'a' get assigned to?
> The if statement "returns" a value so must have both branches.
> However, in a monadic constraint, there are the functions 'when' and
> 'unless.' They allow conditional evaluation of expressions in a
> monadic context. For example,
> main = do
> line <- getLine
> when (line == "hello") putStrLn "Hello back!"
> - Tim
> On Wed, Oct 21, 2009 at 7:43 PM, michael rice <nowgate at yahoo.com
> </mc/compose?to=nowgate at yahoo.com>> wrote:
> It looks like both the THEN and the ELSE in an IF expression
> must each have an expression. What's a graceful way to do
> nothing in either or both slots, kind of like the Fortran
> CONTINUE statement.
> [michael at localhost ~]$ ghci
> GHCi, version 6.10.3: http://www.haskell.org/ghc/ :? for help
> Loading package ghc-prim ... linking ... done.
> Loading package integer ... linking ... done.
> Loading package base ... linking ... done.
> Prelude> if (1==1) then else
> <interactive>:1:15: parse error on input `else'
> Prelude> if (1==1) then True else
> <interactive>:1:24: parse error (possibly incorrect indentation)
> Prelude> if (1==1) then True else False
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org </mc/compose?to=Haskell-Cafe at haskell.org>
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe