除了模式保护之外,为什么不使用谓词保护?
{-# LANGUAGE MultiWayIf, LambdaCase #-}
module Main where
import System.Info (os)
import Control.Applicative ((<$>))
import Data.Char (toLower)
import Data.List (isPrefixOf)
main :: IO ()
main = print . ($ toLower <$> os) $ \x -> if
| "mingw" `isPrefixOf` x -> "windows"
| "darwin" == x -> "mac"
| otherwise -> "linux"
会更漂亮:
main = print $ case toLower <$> os of
x | "mingw" `isPrefixOf` x -> "windows"
| "darwin" == x -> "mac"
| otherwise -> "linux"
甚至:
main = print $ case toLower <$> os of
| ("mingw" `isPrefixOf`) -> "windows"
| ("darwin" ==) -> "mac"
| otherwise -> "linux" -- when pattern absent: otherwise = const True