"Real World Haskell" を読む
日時 2010-08-14(土) 13:00-18:00 場所 タイムインターメディア 2F大会議室
参加はこちらから。
日時 2010-07-31(土) 13:00-18:00 場所 タイムインターメディア 2F大会議室
読書会での質問、回答、コメントのメモです。
2010-05-22
第12章の途中(12.9)から読み進めます。
参加はこちらから。
2010-04-17
前日の 擬データを使った Santa Claus problem の話
第12章
2010-03-27
第10章から第11章まで読了。
2010-01-16
第9章
2009-10-31
第7章の途中から第8章まで読了。
Chaton での実況:http://practical-scheme.net/chaton/haskell-ja/a/2009/10/31#entry-4aebc797-4d367
2009-08-23
第6章と第7章の途中まで
Chaton での実況:http://practical-scheme.net/chaton/haskell-ja/a/2009/08/23
2009-07-20
第5章
2009-06-27
第3章の途中から第4章まで読了。
2009-05-17
1.,2.
numOfElems :: [a] -> Int
numOfElems (_:xs) = 1 + numOfElems xs -- right wing :)
numOfElems _ = 0
numOfElems' :: [a] -> Int
numOfElems' (_:xs) = numOfElems' xs + 1 -- left wing :)
numOfElems' _ = 0
{-
numOfElems = foldr (\ _ l -> 1 + l) 0
numOfElems = foldl' (\ l _ -> l + 1) 0
-}
3.
mean xs = sum xs / fromIntegral (length xs)
{-
-- 1パス
mean = uncurry (/) . foldl' acc (0,0)
where acc (s,l) x = (s+x,l+1)
-}
4.
-- パターンマッチを使う mkPalindrome :: [a] -> [a] mkPalindrome (x:xs) = x : (mkPalindrome xs ++ [x]) mkPalindrome _ = [] -- 以下は蛇足... -- Sコンビネータを使う mkPalindrome' = s (++) reverse s :: (a -> b -> c) -> (a -> b) -> (a -> c) s f g x = f x (g x) -- 与えられたリストを1度だけ辿る(ように見えるだけ?) mkPalindrome'' = uncurry ($) . foldl f (id, []) where f (g,xs) x = (g . (x:), x : xs) mkPalindrome''' xs = foldr (\x b -> b++[x]) xs xs
5.
palindrome :: Eq a => [a] -> Bool palindrome xs = xs == reverse xs -- S palindrome' = s (==) reverse -- 1パス? palindrome'' xs = fst $ foldr check (True,xs) xs where check x (p,y:ys) = (x == y && p,ys)
6.
import Data.Ord (comparing) sortByLength :: [[a]] -> [[a]] sortByLength = map snd . sortBy (comparing snd) . map (s (,) length)
7.,8.
intersperse :: a -> [[a]] -> [a] intersperse s [] = [] intersperse s [x] = [x] intersperse s (x:xs) = x:s: intersperse s xs intersperse' s [] = [] intersperse' s (xs:xss) = xs ++ concatMap (s:) xss
9.
depth (Node _ l r) = 1 + max (depth l) (depth r) depth _ = 0
10.
data Direction = CCW | CW | OnLine
11.
data Vec2D = C Double Double
type Point = Vec2D
sub :: Vec2D -> Vec2D -> Vec2D
sub (C x y) (C x' y') = C (x-x') (y-y')
prd :: Vec2D -> Vec2D -> Double
prd (C x y) (C x' y') = x*y' - x'*y
direction :: Point -> Point -> Point -> Direction
direction x y z = case sign (prd (sub y x) (sub z y)) of
1 -> CCW
0 -> OnLine
_ -> CW
1.
toList :: List a -> [a] toList (Cons x xs) = (:) x (toList xs)
2.
data Tree a = Node (Maybe (Tree a)) (Maybe (Tree a))
2009-04-18