可能是一个愚蠢的问题,但我终生无法弄清楚这一点。

我想根据一系列 if 语句附加到列表的末尾。

在python(或我熟悉的大多数其他语言)中,我可以做这样的事情:

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

这会给我:
['hi', 'hello', 'goodbye']

在 Haskell 中如何实现这样的事情?

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

还是我完全错了?

我对 Haskell 很陌生,所以请不要咬...

最佳答案

惯用语,

x = concat [ [ "hi" ],
             [ "hello" | True ],
             [ "wait a minute..." | not True ],
             [ "goodbye" | True ] ]

关于haskell - haskell 中的一系列独立 if 语句,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25946688/

10-12 23:15
查看更多