Skip to content

Instantly share code, notes, and snippets.

View motokiee's full-sized avatar
🎯
Focusing

motokiee motokiee

🎯
Focusing
View GitHub Profile
@motokiee
motokiee / CodePiece.hs
Created November 10, 2015 05:02
独自のデータ型の定義、Eitherの使い方を勉強。HTTPステータスコードと状態の対応表を導き出すコードを簡単な応用で書いてみた。 #CodePiece
data LockerState = Taken | Free deriving (Show, Eq)
type Code = String
type LockerMap = Map.Map Int (LockerState, Code)
lockerLookup :: Int -> LockerMap -> Either String Code
lockerLookup lockerNumber map = case Map.lookup lockerNumber map of
Nothing -> Left $ "Locker " ++ show lockerNumber ++ " doesn't exist!"
Just (state, code) -> if state /= Taken
then Right code
else Left $ "Locker " ++ show lockerNumber ++ " is already taken!!"
@motokiee
motokiee / CodePiece.hs
Created November 9, 2015 05:00
自動導出とか型シノニムとか #CodePiece
import qualified Data.Map as Map
data Person = Person { firstName :: String,
lastName :: String,
age :: Int
} deriving (Eq, Show, Read)
data Day = Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday deriving (Eq, Ord, Show, Read, Bounded, Enum)
type AssocList k v = [(k, v)]
@motokiee
motokiee / CodePiece.hs
Created November 6, 2015 04:54
久しぶりのランチタイム勉強 #CodePiece
-- data Person = Person String String Int Float String String deriving (Show)
data Person = Person { firstName :: String,
lastName :: String,
age :: Int,
height :: Float,
phoneNumber :: String,
flavor :: String
} deriving (Show)
@motokiee
motokiee / CodePiece.hs
Created October 28, 2015 05:08
あまり進捗はなかった #CodePiece
module Shapes
( Point, Shape, area, nudge, baseCircle, baseRect ) where
-- 値コンストラクタが1つしかない場合、データ型と値コンストラクタは同じ名前にする(慣例)
data Point = Point Float Float deriving (Show)
data Shape = Circle Point Float | Rectangle Point Point deriving (Show)
area :: Shape -> Float
area (Circle _ r) = pi * r ^ 2
area (Rectangle (Point x1 y1) (Point x2 y2)) = (abs $ x2 - x1) * (abs $ y2 - y1)
@motokiee
motokiee / CodePiece.hs
Created October 27, 2015 04:47
自作データ型をはじめて作った #CodePiece
data Shape = Circle Float Float Float | Rectangle Float Float Float Float
deriving (Show)
area :: Shape -> Float
area (Circle _ _ r) = pi * r ^ 2
area (Rectangle x1 y1 x2 y2) = (abs $ x2 - x1) * (abs $ y2 - y1)
@motokiee
motokiee / CodePiece.hs
Created October 27, 2015 04:30
昨日使ったモジュールで体積を表示する的なことをした #CodePiece
import qualified Geometry.Sphere as Sphere
import qualified Geometry.Cuboid as Cuboid
import qualified Geometry.Cube as Cube
printSphere :: Float -> String
printSphere value = show $ Sphere.volume value
printCuboid :: Float -> Float -> Float -> String
printCuboid a b c = show $ Cuboid.volume a b c
@motokiee
motokiee / CodePiece.hs
Created October 26, 2015 05:04
Module作る的なことをやったけど、ghciで確認できてない。。。 #CodePiece
module Geometry
( sphereVolume,
sphereArea,
cubeVolume,
cubeArea,
cuboidArea,
cuboidVolume
) where
-- exportしている関数
@motokiee
motokiee / CodePiece.hs
Created October 20, 2015 04:53
きょうはData.Mapに触れましたよ #CodePiece
import qualified Data.Map as Map
import Data.Char
phoneBook =
[("betty", "555-2938"),
("betty", "342-2492"),
("bonnie", "452-2928"),
("pasty", "493-2928"),
("pasty", "943-2928"),
("pasty", "827-9162"),
@motokiee
motokiee / CodePiece.hs
Created October 19, 2015 04:54
モジュールとかMaybe使って色々遊ぶ的なやつ #CodePiece
import Data.List
import Data.Char
wordNums :: String -> [(String, Int)]
wordNums = map (\ws -> (head ws, length ws)) . group . sort . words
isIn :: (Eq a) => [a] -> [a] -> Bool
needle `isIn` haystack = any (needle `isPrefixOf`) (tails haystack)
encode :: Int -> String -> String
@motokiee
motokiee / CodePiece.hs
Created October 14, 2015 04:52
ちょっとだけHaskellのモジュールの勉強した。ちょっと面倒な処理をこの少ない行にまとめられるのスゴい #CodePiece
import Data.List
import qualified Data.Map as M
numUniques :: (Eq a) => [a] -> Int
numUniques = length . nub
wordNums :: String -> [(String,Int)]
wordNums = map (\ws -> (head ws, length ws)) . group . sort . words