Haskellã®ABC(Haskell Advent Calendar 6th)
Haskellã¨ããã°ä¸æåå¤æ°åãä¸æåå¤æ°åã¨ããã°Haskellã¨ãã{{è¦åºå ¸}}ã»ã©ã«Haskellã§ã¯ä¸æåã®å¤æ°åããã使ããã¦ãããããã¯ååãèããã®ããµãã£ã¦ããã¨ã¯éããªããå¤ç¸æ§ã«ãã£ã¦å¤æ°ãå ·ä½çãªæ§è³ªãæããªããããã«ãå ·ä½çãªååãã¤ããããªãã¨ããã®ã主ãªçç±ã ããããã¯ãé©åãªååããã£ã¦ããæ¢åã®ååã¨è¢«ã£ã¦ããã¨ããå ´åãããããã¨ãã£ã¦å®å ¨ãªã©ã³ãã ã¨ããããã§ããªãã®ã§ãä¸æåå¤æ°åã¯ã©ã®ããã«é¸ã¹ã°ãããèå¯ãã¦ãããã
a
ãããã種: *
ã¢ã«ãã¡ãããã®æåã§ããaã¯æ±ç¨æ§ãé«ããåå¤æ°ã«ä½¿ãã®ãçéã ãå¤ã¬ãã«ã®å¤æ°ã¨ãã¦åä½ã§ä½¿ããã¨ã¯æå¤ã¨å°ãªãã
reverse :: [a] -> [a]
b
ãããã種: *
aã«ç¶ãã¦ãbãåå¤æ°ã«ãã使ãããã
map :: (a -> b) -> [a] -> [b]
c
ä¸ã¤ã®å¤ãä¸ããããã¨ããããããa, b, cã¨ã¤ãããã¨ãå¤ããã¾ããç¶ç¶(continuation)ã«ååãä»ããããk
ã¨cont
ã使ç¨æ¸ã¿ãªå ´åã«ã使ãã
d
ããããå Float, Double
å·®å(difference)ãããã¯è¡åå¼(determinant)ã¨ãã¦ãdx,dyã®ããã«ä»ã®ã¢ã«ãã¡ãããã¨çµã¿åãããå ´åãå¤ãã
e
ä½ãã®è¦ç´ (element)ã§ããã¨ããæ§è³ªã強調ãããã¨ããåãå¤ã¨ãã«ä½¿ããã¨ãããã
f
ãããã種 * -> *
é¢æ°(function)ã表ãã®ã«é常ã«éè¦ã ãAlternative以ä¸ãªé¢æã®ããã®åå¤æ°ã«ããã使ãããã
g
ãããã種 * -> *
fã¨ã»ã¼åãç¨éã§ããã
h
fã¨åã使ãæ¹ãããããé«ã(height)ã¨ãã¦ã®åºçªãå¤ãã
area :: Num a => Rectangle a -> a area (Rectangle w h _ _) = w * h
i
ããããå Int
æ·»å(index)ã表ãå®çªã®æåã§ãè¨èªãåãããã使ããã¦ãããå ¥å(input)ã表ãã®ã«ã使ãã
j
ããããå Int
iã«ç¶ãã¦æ·»åã表ããçè ã¯ã¤ã³ãã¢ã³ã«ä½¿ã£ããã¨ããããããã¾ãããä¾ã¨ã¯è¨ããªãã
forM_ [0..9] $ \i -> forM_ [0..9] $ \j -> putStrLn $ show i ++ " * " ++ show j ++ " = " ++ show (i * j :: Int)
k
第ä¸ã®æ·»åãããã¯ãããã®ãã¼ã®ã»ããHaskellã«ããã¦ã¯ç¶ç¶ã®ååã¨ãã¦ããã使ãããã種å¤æ°ãå¿ãã¦ã¯ãããªãã
answer :: ContT r IO Int answer :: ContT $ \k -> do putStr "Calculating the answer..." k 42 putStrLn "Done."
l
é·ã(length)ã¨ãã¦ãã¾ãã«ãå 容ã«èå³ã®ãªããªã¹ãã®ååã¨ãã¦ä½¿ããã¨ãããã
m
ãããã種 * -> *
ããããå Map k v, Int
ã¢ããã®åå¤æ°ããã¢ããã£ãã¯ãªã¢ã¯ã·ã§ã³ã®ååã¨ãã¦é »ç¹ã«ä½¿ããããæå³Haskellãããå¤æ°åã ãnã¨å ±ã«ãæ´æ°ã®å¤æ°ã¨ãã¦ããã°ãã°åºçªãæ¥ãä»ããããã®ååã¨ãã¦ã使ãã
when :: Monad m => Bool -> m () -> m ()
n
ããããå Int
èªç¶æ°ãªããæ´æ°ã表ããã¨ãå¤ããã¢ãããäºã¤ããã¨ãã«ä½¿ããã¨ãç¨ã«ãã(mmorphãªã©)
o
oã¯å¿é¿ãããå¾åãããããåç¹ããªãã·ã§ã³ãåºåãªã©ããããã使ãéã¯ããã
p
ããããå Double, a -> Bool
ãã©ã¡ã¼ã¿ãä½ç½®(position)ãç¹(point)ãè¿°èª(predicate)ããããããªã©ã«å¼ã£å¼µãã ãã ã
q
pã«ç¶ãã¦ãã©ã¡ã¼ã¿ã¨ãã¦ä½¿ããããã¯ã¨ãªã«ãã
r
ããããå a -> r
çµæã¾ãã¯ç¶ç¶ã表ããããã奪ãããã¨å人çã«ãã¤ãã
newtype ContT r m a = ContT { runContT :: (a -> m r) -> m r }
s
ãããã種 *
ç¶æ (state)ãã¹ããªã¼ã ãæ§é (structure)ãªã©ã«ä½¿ããå人çãªãæ°ã«å ¥ãã ã
type Lens s t a b = forall f. Functor f => (a -> f b) -> s -> f t
t
ããããå Float, Double
æé(time)ãæ¥ç¹ãæ¥ç·(tangent)ãªã©ã
u
ããããå Float, Double
UV座æ¨ç³»ãæ±ãéãvã¨ã®å¯¾ã§ç¨ããã
v
ããããå Float, Double
ãå¤(value)ãã¨ãã¦æ¼ ç¶ã¨ä½¿ããã¨ãããã°ããã¯ãã«(vector)ã表ããã¨ããããè¨èªå¦çç³»ãå®è£ ããéã¯å¤æ°(variable)ã¨ããæå³ã§ãç¨ããã
w
ããããå Float, Double
å¹ (width)ãéã¿(weight)ããããã¯ã¯ãªã¼ã¿ããªã³ã®æåãªã©ãã¢ããã®å対ã§ããã³ã¢ããã®åå¤æ°ã¨ãã¦ã使ãããããã®å¿ã¯ä¸ä¸å転ãããmã
x
ãããã種 *
å¤ã®å¤æ°ã®å®çªä¸ã®å®çªã ãæ¯ãèãã«ä¸åé¢ä¸ããªãåå¤æ°ã«ãç¨ããããã
y
xã¨åæ§ãå¤æ°ã«ä½¿ãã
z
x,yã¨åæ§ãæ±ç¨çãªå¤æ°åã座æ¨ãæ±ãéã¯é »ç¹ã«ç¨ãããrã使ããªãã¨ãã«ä»£ç¨ãããã¨ãã§ããã
ç·è©
æ½è±¡åº¦ã®é«ãã³ã¼ãã«ããã¦ãååãã¤ããã¨ããã®ã¯ãªããªãé£ããåé¡ã®ä¸ã¤ã ã幸ããåå¤æ°å«ãå±æçãªå¤æ°ãªãä¸æåã§ãè¡åã¯æªããªãã®ã§ãæ¢åã®ã©ã¤ãã©ãªãªã©ãåèã«ãã¤ã¤ã¬ã³ã¬ã³çãååã使ã£ã¦ãããã