Skip to content

Instantly share code, notes, and snippets.

@moznion
moznion / myLogic.hs
Created July 29, 2012 12:21
スタートHaskell 2 第2回目演習 論理演算
myNot :: Bool -> Bool
myNot True = False
myNot _ = True
myAnd :: Bool -> Bool -> Bool
myAnd True b = b
myAnd _ _ = False
myOr :: Bool -> Bool -> Bool
myOr False b = b
@moznion
moznion / leap.hs
Created July 29, 2012 12:44
スタートHaskell 2 第2回目演習 うるう年
isLeap :: Int -> Bool
isLeap x
| canBeDividedBy 4 && not (canBeDividedBy 100) = True
| canBeDividedBy 400 = True
| otherwise = False
where
canBeDividedBy y = x `mod` y == 0
@moznion
moznion / fizzBuzz.hs
Created July 29, 2012 12:44
スタートHaskell 2 第2回目演習 FizzBuzz
fizzBuzz :: Int -> String
fizzBuzz x
| fizz && buzz = "FizzBuzz"
| fizz = "Fizz"
| buzz = "Buzz"
| otherwise = show x
where
fizz = x `mod` 3 == 0
buzz = x `mod` 5 == 0
@moznion
moznion / removeCaseIf.hs
Created July 29, 2012 12:45
スタートHaskell 2 第2回目演習 case とif を使わない
analysisLine :: String -> String
analysisLine [] = "empty"
analysisLine (_:[]) = "a character"
analysisLine xs
| last xs == '.' = "a sentence"
| ' ' `elem` xs = "some words"
| otherwise = "a word"
@moznion
moznion / caesar.hs
Created July 29, 2012 12:46
スタートHaskell 2 第2回目演習 シーザー暗号
import Data.Char
charToInt :: Char -> Int
charToInt c
| isLower c = ord c - ord 'a'
| isUpper c = ord c - ord 'A' + 26
| otherwise = ord c
intToChar :: Int -> Char
intToChar x
@moznion
moznion / exhaustivePatternMatch.hs
Created July 29, 2012 12:47
スタートHaskell 2 第2回目演習 パターンマッチの網羅
charName :: Char -> String
charName 'a' = "Albert"
charName 'b' = "Broseph"
charName 'c' = "Cecil"
charName _ = "Special Others"
@moznion
moznion / myLength.hs
Created July 29, 2012 12:48
スタートHaskell 2 第2回目演習 リストの長さ
myLength :: [a] -> Int
myLength [] = 0
myLength (_:xs) = 1 + myLength xs
@moznion
moznion / myCalc.hs
Created July 29, 2012 12:48
スタートHaskell 2 第2回目演習 総和と総積
mySum :: [Int] -> Int
mySum [] = 0
mySum (x:xs) = x + mySum xs
myProduct :: [Int] -> Int
myProduct [] = 1
myProduct (x:xs) = x * myProduct xs
@moznion
moznion / oddEven.hs
Created July 29, 2012 12:49
スタートHaskell 2 第2回目演習 偶数と奇数にわける
oddEven :: [Int] -> ([Int], [Int])
oddEven [] = ([], [])
oddEven (x:xs)
| odd x = connectTuple ([x], []) $ oddEven xs
| otherwise = connectTuple ([], [x]) $ oddEven xs
where
connectTuple :: ([a], [b]) -> ([a], [b]) -> ([a], [b])
connectTuple (x1, y1) (x2, y2) = (x1 ++ x2, y1 ++ y2)
@moznion
moznion / geometricProgression.hs
Created July 29, 2012 12:50
スタートHaskell 2 第2回目演習 等比数列
gp :: Num a => a -> a -> [a]
gp first ratio = [first] ++ gp (first * ratio) ratio