http://tohtml.com/haskell/
I can now have haskell syntax highlight in my blog.
{- Match functions before call. Even type -} data WeekDay = Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday deriving (Eq, Ord, Show, Read, Bounded, Enum) workingDay::WeekDay -> Bool workingDay Saturday = False workingDay Sunday = False workingDay _ = True {- The usage of @ -} isSaturday::[WeekDay] -> String isSaturday [] = "Empty!" isSaturday [_] = "Only one" isSaturday full@(reverse -> (y:_)) = if y == read "Saturday" then show y ++ " is at the end of " ++ show full else "Not Saturday" {- Use where -} overC::(RealFloat a) => a -> a -> a -> String overC a b c | rate > 1 = ">" | rate == 1 = "=" | rate < 1 = "<" where rate = (a^2 + b^2)/c^2 {- Use let -} sumToMax::Int -> Int sumToMax maxV = let s = maxV in if s>0 then s + sumToMax (s-1) else 0 {- Pattern matching in function use case -} isMonday,isTuesday::WeekDay->String isMonday wd = case wd of Monday -> "It is Monday." _ -> "It is not Monday." isTuesday wd | wd == Tuesday = "It is Tuesday" | otherwise = "It is not Tuesday" {- Mix up case with where -} fridayZeroKiller::Int->WeekDay->String fridayZeroKiller x wd = case wd of Friday -> killEven x _ -> "Busy on other day." where killEven 0 = "Killed." killEven _ = "I only kill zero."
0 件のコメント:
コメントを投稿