noritsuguの日記

2009-04-16``Algorithms'' (41) Exercise 2.12

Prelude> [ x | x <- [1..15], x /= 9]
[1,2,3,4,5,6,7,8,10,11,12,13,14,15]
Prelude> [ x * (-1) ^ x | x <- [2..11] ]
[2,-3,4,-5,6,-7,8,-9,10,-11]

[11, 20, 36, 47] を作る方法が分からない…(そもそも規則が分からない…)

MukeshMukesh2012/12/20 19:30That's not even 10 miunets well spent!

svzrxzsvzrxz2012/12/21 14:31xrvvdL <a href="http://cazcscvmnqft.com/">cazcscvmnqft</a>

irhrsystvmvirhrsystvmv2012/12/21 23:06cfgQRg , [url=http://mxhwatrmcdng.com/]mxhwatrmcdng[/url], [link=http://nrebquacnfph.com/]nrebquacnfph[/link], http://yhggyjdzgyju.com/

xfufhhjyinxfufhhjyin2012/12/22 22:48uENsgk <a href="http://vrhwgzsexgpg.com/">vrhwgzsexgpg</a>

トラックバック - http://haskell.g.hatena.ne.jp/noritsugu/20090416

2009-04-15``Algorithms'' (40) Exercise 2.11

Prelude> let compose f g x = f (g x)
Prelude> :t compose
compose :: (t1 -> t2) -> (t -> t1) -> t -> t2

へぇ〜

トラックバック - http://haskell.g.hatena.ne.jp/noritsugu/20090415

2009-04-14``Algorithms'' (39) Exercise 2.10

Prelude> map fst [(1, 2), (3, 8), (0, 6), (3, 1)]
[1,3,0,3]
Prelude> let l     = [6,9,8,3,10]
Prelude> let f x y = (x+y) `div` 2
Prelude> (foldr f 0 l, foldl f 0 l)
(6,7)
Prelude> foldr (++) [] [[1, 2, 3], [4, 5, 6], [], [7]]
[1,2,3,4,5,6,7]
トラックバック - http://haskell.g.hatena.ne.jp/noritsugu/20090414

2009-04-13``Algorithms'' (38) Exercise 2.9

import Data.Char

string2int xs = string2int' (reverse xs)

string2int' (x:xs) = c2int x + 10 * string2int' xs
string2int' []     = 0

c2int c | isDigit   c = digitToInt c
        | otherwise   = 0

で、

*Main> string2int "3454"
3454
*Main> string2int "76"
76

import Data.Char

string2int = foldl (\r x -> c2int x + 10 * r) 0

c2int c | isDigit   c = digitToInt c
        | otherwise   = 0
トラックバック - http://haskell.g.hatena.ne.jp/noritsugu/20090413

2009-04-12``Algorithms'' (37) Exercise 2.8

(a) list comprehension を使って、負の個数を数える

neg xs = length [ x | x <- xs, even x ]

で、

*Main> neg [1, -9, -5, 4, -6, 0]
3

list comprehension だけでできるのかな???


(b)

rep n = concat [ replicate x x | x <- [1..n] ]

で、

*Main> rep 1
[1]
*Main> rep 2
[1,2,2]
*Main> rep 3
[1,2,2,3,3,3]
*Main> rep 4
[1,2,2,3,3,3,4,4,4,4]

list comprehension だけでできるのかな???

トラックバック - http://haskell.g.hatena.ne.jp/noritsugu/20090412