Skip to content

Commit 548cd83

Browse files
committed
Part 1 Chapter 7 Exercise 6 - part 2.
1 parent 395a9b1 commit 548cd83

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

part1_chapter7_exercise6/Main.hs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,29 @@
66
-- ghci
77
--
88
-- :load Main
9+
type Bit = Int
10+
911
main = do
1012
putStrLn "int2bin 9"
1113
putStrLn . show $ int2bin 9
1214

15+
putStrLn "chop8 [1, 2, 3, 4, 5, 6, 7, 8, 9]"
16+
putStrLn . show $ chop8 [1, 2, 3, 4, 5, 6, 7, 8, 9]
17+
18+
putStrLn "chop8' [1, 2, 3, 4, 5, 6, 7, 8, 9]"
19+
putStrLn . show $ chop8' [1, 2, 3, 4, 5, 6, 7, 8, 9]
20+
1321
unfold :: (a -> Bool) -> (a -> a) -> (a -> a) -> a -> [a]
1422
unfold predicate headFunction tailFunction x
1523
| predicate x = []
1624
| otherwise = headFunction x : unfold predicate headFunction tailFunction (tailFunction x)
1725

18-
int2bin = unfold (== 0) (`mod` 2) (`div` 2)
26+
int2bin :: Int -> [Int]
27+
int2bin = unfold (== 0) (`mod` 2) (`div` 2)
28+
29+
chop8 :: [Bit] -> [[Bit]]
30+
chop8 [] = []
31+
chop8 bits = (take 8 bits) : (chop8 (drop 8 bits))
32+
33+
chop8' :: [Bit] -> [[Bit]]
34+
chop8' = unfold null (take 8) (drop 8)

0 commit comments

Comments
 (0)