7

私はそのようなことをするための正しくエレガントな方法を考えていました

function candy = case (color candy) of
    Blue -> if (isTasty candy) then eat candy
            else if (isSmelly candy) then dump candy
            else leave candy

私は試した

function candy = case (color candy) of
    Blue -> dealWith candy
        where dealWith c
                | isTasty c = eat candy
                | isSmelly c = dump candy
                | otherwise = leave candy

これを改善する方法を知っている人はいますか?

もっと

私はこれを使用できることを知っています

function candy = case (color candy) of
    Blue -> case () of
                _ | isTasty candy -> eat candy
                  | isSmelly candy -> dump candy
                  | otherwise -> leave candy

しかし、case何にも一致しない while を使用することは、正しい方法ではないようです。

4

3 に答える 3

16

case外部式でガードを直接使用できます。

fun candy = case color candy of
    Blue | isTasty candy  -> eat candy
         | isSmelly candy -> dump candy
         | otherwise      -> leave candy
于 2013-05-24T14:11:01.440 に答える