Seb*_*edl 11
不,但你可以用守卫表达来做.
fn :: Int -> Int
fn i | i > 0 = (-i)
fn i | otherwise = i
Run Code Online (Sandbox Code Playgroud)
虽然@Sebastian反应是正确的,是的,你可以
{-# LANGUAGE ViewPatterns #-}
import Prelude hiding (odd)
data Peano = Zero | Succ Peano deriving Show
data PeanoInt = Neg Peano | Pos Peano deriving Show
odd :: PeanoInt -> Bool
odd (Neg Zero) = False
odd (Pos Zero) = False
odd (Neg (Succ (Succ x))) = odd $ Neg x
odd (Pos (Succ (Succ x))) = odd $ Pos x
odd _ = True
zero = Zero
one = Succ zero
two = Succ one
f :: PeanoInt -> String
f (Neg (Succ (Succ Zero))) = "-2 (then we can match all finite sets)"
f (Pos _) = "Positives"
f (odd -> True) = "Odd!"
f x = show x
main = do
print $ f (Neg two)
print $ f (Pos one)
print $ odd (Neg one)
print $ odd (Neg two)
print $ odd (Pos one)
print $ odd (Pos two)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
807 次 |
| 最近记录: |