git.iko.soy/production-haskell
on master help sign in
.
commit745877b3d876d3812ab64f899a370808efe96f37 authoriko <mail@iko.soy> dateWed 22 Jan 2025, 17:14 (2 years ago) parentf7a77e8
1 file changed+29−9
seminars/1/src/Task.hs+29−9
seminars/1/src/Task.hs+29−9
@@ -75,7 +75,8 @@ stringListHead x = case x of
7575-- >>> safeHead []
7676-- Nothing
7777safeHead :: [a] -> Maybe a
78−safeHead = error "TODO: safeHead"
78+safeHead [] = Nothing
79+safeHead (x:_) = Just x
7980
8081-- Same as `safeHead`, but returns the last element.
8182--
@@ -85,7 +86,9 @@ safeHead = error "TODO: safeHead"
8586-- >>> safeLast []
8687-- Nothing
8788safeLast :: [a] -> Maybe a
88−safeLast = error "TODO: safeLast"
89+safeLast [] = Nothing
90+safeLast [x] = Just x
91+safeLast (_:xs) = safeLast xs
8992
9093-- Returns the same list without the last element.
9194--
@@ -95,7 +98,9 @@ safeLast = error "TODO: safeLast"
9598-- >>> withoutLast []
9699-- []
97100withoutLast :: [a] -> [a]
98−withoutLast = error "TODO: withoutLast"
101+withoutLast [] = []
102+withoutLast [x] = []
103+withoutLast (x:xs) = x:(withoutLast xs)
99104
100105-- Returns the element at a given index if there is one.
101106-- Otherwise, return Nothing.
@@ -106,15 +111,19 @@ withoutLast = error "TODO: withoutLast"
106111-- >>> indexed 8 [6, 5, 4]
107112-- Nothing
108113indexed :: Natural -> [a] -> Maybe a
109−indexed = error "TODO: indexed"
114+indexed 0 (x:_) = Just x
115+indexed number [] = Nothing
116+indexed number (_:xs) = indexed (number-1) xs
110117
111118-- Is an infinite list, alternating the sign of every other element.
112119--
113120-- >>> alternating
114121-- [1, -2, 3, -4, 5, -6, ...
115122alternating :: [Integer]
116−alternating = error "TODO: alternating"
117−
123+alternating = cut rising
124+ where
125+ cut (x1:x2:xs) = x1:(-x2):cut xs
126+ rising = 1 : map (+ 1) rising
118127-- ____ _ _
119128-- | __ )(_)_ __ __ _ _ __ _ _ | |_ _ __ ___ ___
120129-- | _ \| | '_ \ / _` | '__| | | | | __| '__/ _ \/ _ \
@@ -138,8 +147,9 @@ alternating = error "TODO: alternating"
138147-- (in this context a 'branch' and a 'tree' is the same thing)
139148--
140149-- In case you didn't get it, a tree should have two constructors.
141−data Tree a -- complete the implementation
150+data Tree a = Branch a (Tree a) (Tree a) | EmptyBranch
142151 deriving (Show, Eq)
152+
143153-- ^ Don't worry about this 'deriving' business. It is needed for tests
144154-- to compile. It will be explained later.
145155
@@ -176,7 +186,15 @@ data Tree a -- complete the implementation
176186-- l l r r
177187-- .............
178188constructTree :: [a] -> Tree a
179−constructTree = error "TODO: constructTree"
189+constructTree [] = EmptyBranch
190+constructTree (x:xs) = Branch x (constructTree leftBranch) (constructTree rightBranch)
191+ where
192+ split (a:b:xs) = (a:aTail, b:bTail)
193+ where
194+ (aTail, bTail) = split xs
195+ split [] = ([], [])
196+ split [x] = ([x], [])
197+ (leftBranch, rightBranch) = split xs
180198
181199-- Returns the leftmost element of the given tree.
182200--
@@ -192,7 +210,9 @@ constructTree = error "TODO: constructTree"
192210-- / \ /
193211-- 4 6 5
194212leftmost :: Tree a -> Maybe a
195−leftmost = error "TODO: leftmost"
213+leftmost EmptyBranch = Nothing
214+leftmost (Branch a EmptyBranch _) = Just a
215+leftmost (Branch _ br _) = leftmost br
196216
197217-- Returns the rightmost element of the given tree.
198218--