How to understand such a `case` ?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

How to understand such a `case` ?

z_axis
findHelper (x:xs) = do -- not lazy, but that's not really important here
        filex  <- fileExists (file  x)
        filex' <- fileExists (file' x)
        case () of
            _
                | filex     -> return $ Just $ file  x
                | filex'    -> return $ Just $ file' x
                | otherwise -> findHelper xs
    file  x = foldl1 joinFileName (x ++ [helper])
    file' x = (file x) ++ (getConfig "exe_ext")

Sincerely!
e^(π.i) + 1 = 0
Reply | Threaded
Open this post in threaded view
|

Re: How to understand such a `case` ?

John Millikin
Sorry; forgot to CC the list

That case is equivalent to:

if filex
   then return $ Just $ file  x
   else if filex'
       then return $ Just $ file' x
       else findHelper xs


The specific syntax being used is called a "pattern guard":
http://www.haskell.org/haskellwiki/Pattern_guard

On Tue, Dec 8, 2009 at 16:59, zaxis <[hidden email]> wrote:

>
> findHelper (x:xs) = do -- not lazy, but that's not really important here
>        filex  <- fileExists (file  x)
>        filex' <- fileExists (file' x)
>        case () of
>            _
>                | filex     -> return $ Just $ file  x
>                | filex'    -> return $ Just $ file' x
>                | otherwise -> findHelper xs
>    file  x = foldl1 joinFileName (x ++ [helper])
>    file' x = (file x) ++ (getConfig "exe_ext")
>
> Sincerely!
>
> -----
> fac n = foldr (*) 1 [1..n]
> --
> View this message in context: http://old.nabble.com/How-to-understand-such-a-%60case%60---tp26703526p26703526.html
> Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
>
> _______________________________________________
> Haskell-Cafe mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: How to understand such a `case` ?

z_axis
thanks for your quick answer ! Then what's the advantage using such a `case` not using `if` statement given by you ?   For me, the `if` statement is more clear .

jmillikin wrote
Sorry; forgot to CC the list

That case is equivalent to:

if filex
   then return $ Just $ file  x
   else if filex'
       then return $ Just $ file' x
       else findHelper xs


The specific syntax being used is called a "pattern guard":
http://www.haskell.org/haskellwiki/Pattern_guard

On Tue, Dec 8, 2009 at 16:59, zaxis <z_axis@163.com> wrote:
>
> findHelper (x:xs) = do -- not lazy, but that's not really important here
>        filex  <- fileExists (file  x)
>        filex' <- fileExists (file' x)
>        case () of
>            _
>                | filex     -> return $ Just $ file  x
>                | filex'    -> return $ Just $ file' x
>                | otherwise -> findHelper xs
>    file  x = foldl1 joinFileName (x ++ [helper])
>    file' x = (file x) ++ (getConfig "exe_ext")
>
> Sincerely!
>
> -----
> fac n = foldr (*) 1 [1..n]
> --
> View this message in context: http://old.nabble.com/How-to-understand-such-a-%60case%60---tp26703526p26703526.html
> Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe@haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
e^(π.i) + 1 = 0
Reply | Threaded
Open this post in threaded view
|

Re: How to understand such a `case` ?

Daniel Fischer-4
In reply to this post by z_axis
Am Mittwoch 09 Dezember 2009 01:59:21 schrieb zaxis:
> findHelper (x:xs) = do -- not lazy, but that's not really important here
>         filex  <- fileExists (file  x)
>         filex' <- fileExists (file' x)
>         case () of
>             _
>
>                 | filex     -> return $ Just $ file  x
>                 | filex'    -> return $ Just $ file' x
>                 | otherwise -> findHelper xs

Such a 'case' is a typographically more pleasant way to test multiple alternatives than a
nested if-then-else chain.

With an if-then-else chain, the code would wander to the right and be less easily
followed. Using a

case () of
  _ | condition1 -> thing1
    | condition2 -> thing2
    | condition3 -> thing3
    ...

you can nicely align all possibilities. Since the 'case' here is extraneous to the code
logic and serves only aesthetic ends, such a practice may be frowned upon.

>
>     file  x = foldl1 joinFileName (x ++ [helper])
>     file' x = (file x) ++ (getConfig "exe_ext")
>
> Sincerely!
>


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: How to understand such a `case` ?

Daniel Fischer-4
In reply to this post by z_axis
Am Mittwoch 09 Dezember 2009 02:26:11 schrieb zaxis:
> thanks for your quick answer ! Then what's the advantage using such a
> `case` not using `if` statement given by you ?   For me, the `if` statement
> is more clear .

Once you have a lot of possibilities to check, if-then-else cascades become rather
unreadable. However, it might then be advisable to rethink your code, perhaps introduce a
choice combinator.
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe