[Haskell-beginners] if True than let...

Daniel Fischer daniel.is.fischer at web.de
Thu Jun 25 16:40:34 EDT 2009


Am Donnerstag 25 Juni 2009 22:12:08 schrieb Bernhard Lehnert:
> Hi,
>
> I'm sorry because I am absolutely sure, this is bloody obvious to the
> knowing. Being a total beginner I'm stuck. In the main = do part I
> wrote:
>
> 1:     if a == True then putStrLn "Yes!" else putStrLn "No."
> 2:     if a == True then let b = "+" else let b = "-"
>
> Line #1 works perfectly well.
> Read line #2 as pseudocode and you'll see what I want to do. Read it in
> ghci and it produces
>      "  parse error on input `='    "
>
> I tried 'case of' but it doesn't work either.
>
> What am I doing wrong?
> Thank you for any help,
>
> Bernhard
>

let b = "+"

is not a complete expression, thus cannot be a branch of 'if'. It should give a parse 
error on 'else' (and my ghci does).
But (putStrLn "Yes!") is a complete expression, so can be used as a branch of an if-
expression.

You probably want to bind the name b to a value depending on a somewhere in main's do-
block?

That would be achieved so:

let b = if a then "+" else "-"

b can then be used in following statements.

Outside of a do-block, you would have to write

let b = if a then "+" else "-" in someExpression

HTH,
Daniel


More information about the Beginners mailing list