2009-04-21``Algorithms'' (46) 3.1.3 Lazy data structures
add x y = x + y double x = add x x data List a = Cons a (List a) | Nil my_map f Nil = Nil my_map f (Cons x xs) = Cons (f x) (my_map f xs) my_head (Cons x xs) = x
で、
*Main> my_head (my_map double (Cons 1 (Cons 2 (Cons 3 Nil)))) 2
add x y = x + y double x = add x x data List a = Cons !a (List a) | Nil my_map f Nil = Nil my_map f (Cons x xs) = Cons (f x) (my_map f xs) my_head (Cons x xs) = x
で、
*Main> my_head (my_map double (Cons 1 (Cons 2 (Cons 3 Nil)))) 2
add x y = x + y double x = add x x data List a = Cons a !(List a) | Nil my_map f Nil = Nil my_map f (Cons x xs) = Cons (f x) (my_map f xs) my_head (Cons x xs) = x
で、
*Main> my_head (my_map double (Cons 1 (Cons 2 (Cons 3 Nil)))) 2
コメントを書く
トラックバック - http://haskell.g.hatena.ne.jp/noritsugu/20090421
2009-04-20``Algorithms'' (45) 3.1.2 Controlling evaluation order in Haskell
Prelude> let add x y = x + y Prelude> let double x = add x x Prelude> double $! (5*4) 40
トラックバック - http://haskell.g.hatena.ne.jp/noritsugu/20090420
2009-04-19``Algorithms'' (44) Exercise 2.15
型の定義。本当は自分で定義する問題だけど
Prelude> :m Array Prelude Array> :t (!) (!) :: (Ix i) => Array i e -> i -> e Prelude Array> :t bounds bounds :: (Ix i) => Array i e -> (i, i) Prelude Array> :t indices indices :: (Ix i) => Array i e -> [i] Prelude Array> :t elems elems :: (Ix i) => Array i e -> [e]
トラックバック - http://haskell.g.hatena.ne.jp/noritsugu/20090419
2009-04-18``Algorithms'' (43) Exercise 2.14
型の定義
cube :: Int -> Int cube x = x * x * x maxi :: Int -> Int -> Int maxi x y | x >= y = x | otherwise = y sumAtoB :: Int -> Int -> Int sumAtoB a b = sum [a..b]
で、
*Main> cube 2 8 *Main> maxi 1 2 2 *Main> sumAtoB 1 10 55
トラックバック - http://haskell.g.hatena.ne.jp/noritsugu/20090418
2009-04-17``Algorithms'' (42) Exercise 2.13
(a) 文の意味が良く分かっていない…
マトリックスの要素に番号を付けて定義しろという意味???
(b) あえて list 〜 でなく、array comprehension と書いてあるんだよな?
行列の行・列どちらを先にすべきか忘れてしまったので適当
Prelude> :m Array Prelude Array> array ((0, 0), (2, 2)) [((i, j), (i * 3 + j + 2)) | i <- [0..2], j <- [0..2]] array ((0,0),(2,2)) [((0,0),2),((0,1),3),((0,2),4),((1,0),5),((1,1),6),((1,2),7),((2,0),8),((2,1),9),((2,2),10)]
(c)
import Array transpose a = a // [((0, 1), a ! (1, 0)), ((0, 2), a ! (2, 0)), ((1, 0), a ! (0, 1)), ((1, 2), a ! (2, 1)), ((2, 0), a ! (0, 2)), ((2, 1), a ! (1, 2))]
で、
*Main> let a = array ((0, 0), (2, 2)) [((i, j), (i * 3 + j + 2)) | i <- [0..2], j <- [0..2]] *Main> transpose a Loading package haskell98 ... linking ... done. array ((0,0),(2,2)) [((0,0),2),((0,1),5),((0,2),8),((1,0),3),((1,1),6),((1,2),9),((2,0),4),((2,1),7),((2,2),10)]
(d) 任意の大きさの Array に拡張せよ
import Array transpose a n = a // [((i, j), a ! (j, i)) | i <- [0..n-1], j <- [0..n-1], i /= j]
で、
*Main> let a = array ((0, 0), (2, 2)) [((i, j), (i * 3 + j + 2)) | i <- [0..2], j <- [0..2]] *Main> transpose a 3 array ((0,0),(2,2)) [((0,0),2),((0,1),5),((0,2),8),((1,0),3),((1,1),6),((1,2),9),((2,0),4),((2,1),7),((2,2),10)]
大きさ n は array から出せそうな気もするけど〜
トラックバック - http://haskell.g.hatena.ne.jp/noritsugu/20090417