Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A series of independent if statements in haskell

Probably a silly question but I can't for the life of me figure this out.

I want to append to the end of a list based on a series of if statements.

In python (or most other languages I am familiar with) I could do something like this:

x = ["hi"]
if True:
    x.append("hello")
if not True:
    x.append("wait a minute...")
if True:
    x.append("goodbye")

Which would give me:

['hi', 'hello', 'goodbye']

How does one achieve such a thing in Haskell?

I can get as far as:

res :: [[Char]]
res =
    let x = ["Hi"]
    in
        if (True)
            then x ++ ["hello"]
            ... what goes here???
        else x

Or am I going about this totally wrong?

I am very new to Haskell so please don't bite...

like image 856
tanbog Avatar asked Oct 19 '25 12:10

tanbog


1 Answers

Idiomatically,

x = concat [ [ "hi" ],
             [ "hello" | True ],
             [ "wait a minute..." | not True ],
             [ "goodbye" | True ] ]
like image 120
luqui Avatar answered Oct 22 '25 04:10

luqui



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!