Skip to content

Instantly share code, notes, and snippets.

View monzou's full-sized avatar

Takuro Monji monzou

  • Tokyo
View GitHub Profile
@monzou
monzou / sc1.png
Created June 10, 2012 06:49
Donuts README images
dummy
@monzou
monzou / qsort2.hs
Created June 16, 2012 08:56
qsort @ Haskell
main = print $ quicksort [ 10, 2, 5, 3, 1, 6, 7, 2, 3, 4, 8, 9 ]
quicksort :: (Ord a) => [a] -> [a]
quicksort [] = []
quicksort (x:xs) =
let
le = filter (<= x) xs
gt = filter (> x) xs
in quicksort le ++ [x] ++ quicksort gt
main = print largest
largest :: Integer
largest = head (filter p [ 100000, 99999 .. ])
where p x = x `mod` 3829 == 0
@monzou
monzou / word_count.hs
Created June 17, 2012 02:21
word-count @ Haskell
import Data.List
main = print $ wordCount "boom bip bip boom bang boom bang"
wordCount :: String -> [(String, Int)]
wordCount = map (\w -> (head w, length w)) . group . sort . words
import Data.Char
main = print $ decode 3 $ encode 3 "hello, world !"
encode :: Int -> String -> String
encode offset msg = map (\c -> chr $ ord c + offset) msg
decode :: Int -> String -> String
decode shift msg = encode (negate shift) msg
import Data.Char
import Data.List
main = print $ firstTo 40
digitSum :: Int -> Int
digitSum = sum . map digitToInt . show
firstTo :: Int -> Maybe Int
firstTo n = find (\x -> digitSum x == n) [ 1.. ]
import qualified Data.Map as Map
data LockerState = Taken | Free deriving (Show, Eq)
type Code = String
type LockerMap = Map.Map Int (LockerState, Code)
lookupLocker :: Int -> LockerMap -> Either String Code
lookupLocker lockerNumber map = case Map.lookup lockerNumber map of
Nothing
-> Left $ "Locker " ++ show lockerNumber ++ " doesn't exist."
data Tree a = EmptyTree | Node a (Tree a) (Tree a) deriving (Show)
emptyNode :: a -> Tree a
emptyNode x = Node x EmptyTree EmptyTree
insert :: (Ord a) => a -> Tree a -> Tree a
insert x EmptyTree = emptyNode x
insert x (Node a left right)
| x == a = Node x left right
| x < a = Node a (insert x left) right
-- 型クラス
-- Maybe 型を Eq 型クラスのインスタンスにする
instance (Eq m) => Eq (Maybe m) where
Just x == Just y = x == y
Nothing == Nothing = True
_ == _ = False
-- Javascript っぽい真偽値判定型を定義する
class YesNo a where
yesno :: a -> Bool
instance YesNo Int where
yesno 0 = False
yesno _ = True
instance YesNo [a] where
yesno [] = False