This file contains hidden or 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
dummy |
This file contains hidden or 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
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 |
This file contains hidden or 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
main = print largest | |
largest :: Integer | |
largest = head (filter p [ 100000, 99999 .. ]) | |
where p x = x `mod` 3829 == 0 |
This file contains hidden or 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
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 |
This file contains hidden or 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
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 |
This file contains hidden or 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
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.. ] |
This file contains hidden or 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
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." |
This file contains hidden or 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
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 |
This file contains hidden or 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
-- 型クラス | |
-- Maybe 型を Eq 型クラスのインスタンスにする | |
instance (Eq m) => Eq (Maybe m) where | |
Just x == Just y = x == y | |
Nothing == Nothing = True | |
_ == _ = False |
This file contains hidden or 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
-- Javascript っぽい真偽値判定型を定義する | |
class YesNo a where | |
yesno :: a -> Bool | |
instance YesNo Int where | |
yesno 0 = False | |
yesno _ = True | |
instance YesNo [a] where | |
yesno [] = False |