Created
March 22, 2013 11:50
-
-
Save odaillyjp/5220714 to your computer and use it in GitHub Desktop.
2012 Start Haskell#2
http://wiki.haskell.jp/Workshop/StartHaskell/LYHGG/exercise/3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# OPTIONS -Wall #-} | |
-- Not,And,Or,Xor を独自実装 | |
myAnd :: Bool -> Bool -> Bool | |
myAnd True True = True | |
myAnd _ _ = False | |
myNot :: Bool -> Bool | |
myNot False = True | |
myNot _ = False | |
myOr :: Bool -> Bool -> Bool | |
myOr True False = True | |
myOr False True = True | |
myOr True True = True | |
myOr _ _ = False | |
myXor :: Bool -> Bool -> Bool | |
myXor True False = True | |
myXor False True = True | |
myXor _ _ = False | |
-- うるう年判定 | |
isLeap :: Int -> Bool | |
isLeap i | |
| i `mod` 400 == 0 = True | |
| i `mod` 100 == 0 = False | |
| i `mod` 4 == 0 = True | |
| otherwise = False | |
-- FizzBuzz | |
fizzBuzz :: Int -> String | |
fizzBuzz x | |
| x `mod` 15 == 0 = "FizzBuzz" | |
| x `mod` 3 == 0 = "Fizz" | |
| x `mod` 5 == 0 = "Buzz" | |
| otherwise = show x | |
-- 文字列解析 | |
analysisLine :: String -> String | |
analysisLine [] = "empty" | |
analysisLine [_] = "a character" | |
analysisLine s' | |
| last s' == '.' = "a sentence" | |
| ' ' `elem` s' = "some words" | |
| otherwise = "a word" | |
-- cancat を独自実装 | |
concatR :: [[a]] -> [a] | |
concatR = foldr (++) [] | |
concatL :: [[a]] -> [a] | |
concatL = foldl (++) [] | |
-- シーザー暗号化 | |
caesarSucc :: Char -> Char | |
caesarSucc 'z' = 'a' | |
caesarSucc 'Z' = 'A' | |
caesarSucc ' ' = ' ' | |
caesarSucc c = succ c | |
caesar :: Int -> String -> String | |
caesar 0 s = s | |
caesar i s = caesar (i-1) . map caesarSucc $ s | |
-- シーザー複合化 | |
caesarPred :: Char -> Char | |
caesarPred 'a' = 'z' | |
caesarPred 'A' = 'Z' | |
caesarPred ' ' = ' ' | |
caesarPred c = pred c | |
caesarComposite :: Int -> String -> String | |
caesarComposite 0 s = s | |
caesarComposite i s = caesarComposite (i-1) . map caesarPred $ s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment