-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeek 4
More file actions
93 lines (61 loc) · 1.95 KB
/
Week 4
File metadata and controls
93 lines (61 loc) · 1.95 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
--Assignment 1
module DFA where
import Prelude hiding (Word)
type State = Int
type Alphabet a = [a]
type DFA a =
( Alphabet a -- alphabet
, State -- initial state
, State -> a -> State -- transition function
, State -> Bool) -- test for final state
type Word a = [a]
alphabet :: DFA a -> Alphabet a
alphabet (a,b,c,d) = a
initial :: DFA a -> State
initial (a,b,c,d) = b
transition :: DFA a -> (State -> a -> State)
transition (a,b,c,d) = c
finalState :: DFA a -> State -> Bool
finalState (a,b,c,d) = d
{-
Please indicate briefly why using accessor functions is useful.
...
-}
accepts :: DFA a -> Word a -> Bool
accepts dfa w = if finalState dfa ( foldl (transition dfa) (initial dfa) w) then True
else False
lexicon :: Alphabet a -> Int -> [Word a]
lexicon _ 0 = [[]]
lexicon a n = [x:xs | x<-a , xs <- (lexicon a (n-1))]
language :: DFA a -> Int -> [Word a]
language dfa n = aux dfa (lexicon (alphabet dfa) n)
aux :: DFA a -> [Word a] -> [Word a]
aux _ [] = []
aux dfa (w:ws) = if accepts dfa w then w:(aux dfa ws) else aux dfa ws
-- Try to use map, foldl, foldr, filter and/or list comprehensions.
--Assignment 2
module ConcatMap where
import Prelude hiding (sum)
concatMap' f = foldr aux e
where aux = ((++) . f)
e = []
--Assignment 3
module Redefine where
{-
In this exercise you are required to adapt the following function
definitions of f, g and h such that foldl, foldr, zip, zipWith, filter,
curry, uncurry, etc. will be used. That means, your task is to modify the
lines 11-12, 16-20 and 24-29.
-}
f :: [[a]] -> [a]
f [] = []
f (x:xs) = foldr ((++).reverse) [] (x:xs)
g :: Eq a => [a] -> [a] -> [a]
g [] _ = []
g _ [] = []
g (x:xs) (y:ys) = [x | (x,y) <- zip (x:xs) (y:ys), x==y]
h :: [Int] -> Int
h = aux 0
where
aux z [] = z
aux z (x:xs) = length [a | a<-(x:xs), even a]