-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeek 6
More file actions
67 lines (46 loc) · 1.49 KB
/
Week 6
File metadata and controls
67 lines (46 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
--Assignment 1
module FileSystemEntries where
data FSEntry = Folder String [FSEntry] | File String String
-- (a)
fFSE :: (String -> [a] -> a) -> (String -> String -> a) -> FSEntry -> a
fFSE fFolder fFile fse = go fse
where
go(Folder s1 fses) = fFolder s1 (map go fses)
go(File s1 s2) = fFile s1 s2
-- (b)
number :: FSEntry -> Int
number fse = fFSE (\s1 ls -> 1+sum ls) (\s1 s2 -> 1) fse
count :: Char -> FSEntry -> Int
count ch fse = fFSE (\s1 ls -> sum ls) (\s1 s2 -> countChar ch s2) fse
countChar :: Char -> String -> Int
countChar _ [] = 0 -- Base case
countChar c (ch:chs) = if c == ch
then 1 + countChar c chs
else countChar c chs
-- (c)
paths :: FSEntry -> [String]
paths = undefined
--Assignment 2
module NQueens where
-- Implement a function that generates all possible board assignments.
generate :: Int -> [[Int]]
generate n = go n n
where
go 0 _ = [[]]
go iter n = [q:qs | q<-[1..n], qs <- go (iter-1) n]
countdown :: Int -> [Int]
countdown 0 = [0]
countdown n = n:(countdown (n-1))
-- Implement a function that tests whether a given assignment is valid.
test :: [Int] -> Bool
test = undefined
naivequeens :: Int -> [[Int]]
naivequeens n = filter test $ generate n
{-
Headache of the week:
Implement a function that solves this n queens problem in a more efficient way
such that partial assignments get tested (whether they may be a valid full
assignment) as early as possible.
-}
queens :: Int -> [[Int]]
queens = undefined