O excursie de weekend in Algonquin Park, profitind de singura zi frumoasa.
Apropiindu-ne de parc pe muzica lui Neil Young:
Tea Lake Dam Trail
Hardwood Lookout Trail
Smoke Lake
Peck Lake
3 octombrie 2011
27 septembrie 2011
18 septembrie 2011
Schimbare de directie
OK, cred ca renunt la ideea de tine blogul asta numai pt software. Motivele:
1) N-am unde sa-mi pun acele poze care erau pe contul de flickr expirat si care n-ar avea sens sa apara in jurnalele de calatorie.
2) Interesul pt Haskell mi-a scazut (sper ca temporar), in principal din cauza ca nu vad nicio ocazie sa folosesc Haskell pt un proiect real.
3) Numele blogului e prea general ca sa fie doar pentru software.
1) N-am unde sa-mi pun acele poze care erau pe contul de flickr expirat si care n-ar avea sens sa apara in jurnalele de calatorie.
2) Interesul pt Haskell mi-a scazut (sper ca temporar), in principal din cauza ca nu vad nicio ocazie sa folosesc Haskell pt un proiect real.
3) Numele blogului e prea general ca sa fie doar pentru software.
9 ianuarie 2011
19 martie 2010
Haskell exercises - Chapter 7
My solutions to exercises from Graham Hutton's Programming in Haskell.
Exercise 7.1
Exercise 7.2
Exercise 7.3
Exercise 7.4
Exercise 7.5
Exercise 7.6
Exercise 7.7
Exercise 7.8
To encode:
To decode:
Then:
Exercise 7.9
Exercise 7.1
foo :: (a -> b) -> (a -> Bool) -> [a] -> [b]
foo f p xs = [f x | x <- xs, p x]
foo' :: (a -> b) -> (a -> Bool) -> [a] -> [b]
foo' f p xs = map f (filter p xs)
-- For example, the following two expressions generate the same list, [1, 9, 25, 49, 81, ...]
sqrodds = [(^2) n | n <- [1..], odd n]
sqrodds' = map (^2) $ filter odd [1..]
Exercise 7.2
all' :: (a -> Bool) -> [a] -> Bool
all' p = foldr (\x y -> p x && y) True
any' :: (a -> Bool) -> [a] -> Bool
any' p = foldr (\x y -> p x || y) False
takeWhile' :: (a -> Bool) -> [a] -> [a]
takeWhile' _ [] = []
takeWhile' p (x:xs) | p x = x : takeWhile' p xs
| otherwise = []
dropWhile' :: (a -> Bool) -> [a] -> [a]
dropWhile' _ [] = []
dropWhile' p (x:xs) | p x = dropWhile' p xs
| otherwise = (x:xs)
Exercise 7.3
map' :: (a -> b) -> [a] -> [b]
map' f = foldr (\x ys -> f x : ys) []
filter' :: (a -> Bool) -> [a] -> [a]
filter' p = foldr (\x ys -> if p x then x : ys else ys) []
Exercise 7.4
dec2int :: [Int] -> Int
dec2int = foldl (\x y -> x*10 + y) 0
Exercise 7.5
-- Answer: The list passed to compose is invalid because its elements are not all of the same type.
Exercise 7.6
curry' :: ((a, b) -> c) -> a -> b -> c
curry' f = \x y -> f (x, y)
uncurry' :: (a -> b -> c) -> (a, b) -> c
uncurry' f = \(x, y) -> f x y
Exercise 7.7
unfold :: (a -> Bool) -> (a -> b) -> (a -> a) -> a -> [b]
unfold p h t x | p x = []
| otherwise = h x : unfold p h t (t x)
type Bit = Int
chop8' :: [Bit] -> [[Bit]]
chop8' = unfold null (take 8) (drop 8)
map' :: (a -> b) -> [a] -> [b]
map' f = unfold null (f . head) tail
iterate' :: (a -> a) -> a -> [a]
iterate' f = unfold (\_ -> False) id f
Exercise 7.8
To encode:
parityBit :: [Bit] -> Bit
parityBit bits = if odd ones then 1 else 0
where ones = sum [b | b <- make8 bits]
make9 :: [Bit] -> [Bit]
make9 bits = (make8 bits) ++ [parityBit bits]
encodePb :: String -> [Bit]
encodePb = concat . map (make9 . int2bin . ord)
To decode:
chop9 :: [Bit] -> [[Bit]]
chop9 = unfold null (take 9) (drop 9) -- where unfold has been defined in Exercise 7.7
dropPb :: [Bit] -> [Bit]
dropPb bits = if last bits == parityBit bits
then take 8 bits
else error "Parity bit error!"
decodePb :: [Bit] -> String
decodePb = (map (chr . bin2int . dropPb)) . chop9
Then:
transmitPb :: String -> String
transmitPb = decodePb . channel . encodePb
where channel = id
Exercise 7.9
transmitPb' :: String -> String
transmitPb' = decodePb . fawltyChannel . encodePb
where fawltyChannel = tail
2 martie 2010
Haskell exercises - Chapter 6
My solutions to exercises from Graham Hutton's Programming in Haskell.
Exercise 6.1
Exercise 6.3
Exercise 6.4
Exercise 6.5
Exercise 6.6
Exercise 6.1
import Prelude hiding ( (^) )
(^) :: Int -> Int -> Int
n ^ 0 = 1
n ^ (m+1) = n * (n^m)
Exercise 6.3
import Prelude hiding (and, concat, replicate, (!!), elem)
and :: [Bool] -> Bool
and [] = True
and (b:bs) = b && and bs
concat :: [[a]] -> [a]
concat [] = []
concat (xs:xss) = xs ++ concat xss
replicate :: Int -> a -> [a]
replicate 0 x = []
replicate (n+1) x = x : replicate n x
(!!) :: Integral a => [a] -> n -> a
(!!) (x:xs) 0 = x
(!!) (x:xs) (n+1) = xs !! n
elem :: Eq a => a -> [a] -> Bool
elem x [] = False
elem x (y:ys) = if x == y then True else elem x ys
Exercise 6.4
merge :: Ord a => [a] -> [a] -> [a]
merge xs [] = xs
merge [] ys = ys
merge (x:xs) (y:ys) = if x < y then x : merge xs (y:ys) else y : merge (x:xs) ys
Exercise 6.5
halve :: [a] -> ([a], [a])
halve xs = (fsthalf, sndhalf)
where fsthalf = take ((length xs) `div` 2) xs
sndhalf = drop (length fsthalf) xs
msort :: Ord a => [a] -> [a]
msort [] = []
msort [x] = [x]
msort xs = merge (msort firsthalf) (msort secondhalf)
where
firsthalf = fst halves
secondhalf = snd halves
halves = halve xs
Exercise 6.6
import Prelude hiding (sum, take, last)
sum :: Num a => [a] -> a
sum [] = 0
sum (x:xs) = x + sum xs
take :: Int -> [a] -> [a]
take 0 xs = xs
take (n+1) (x:xs) = x : take n xs
last :: [a] -> a
last [x] = x
last (x:xs) = last xs
Abonați-vă la:
Postări (Atom)
