在上 FLOLAC 的課程時,老師出了一些 Haskell 的練習題給我們練習。
這邊我列出第三部分的題目跟我作的參考答案~ (練習使用 List 跟寫遞回函數!)
但我還是建議你看原版的PDF (有全部的題目) 會比較清楚XD
When I am studying in the FLOLAC, the teacher gave us some Haskell exercises to practice.
I put the problems and my answer of section 3 to this article! ( for List and recursive functions.)
But I think you should see the original practicals PDF, which is more clear.
1. Define a function fstEven :: [Int] → Int that returns the first even number of the input list.
2. Define a function hasZero :: [Int] → Bool that returns True if and only if there is a 0 in the input list.
3. Define a function myLast that takes a list and returns the last (rightmost) element.
(a) Let the type be myLast :: [a] → a. Define myLast.
(b) What happens in the previous definition of the input list is empty?
(c) Define myLast :: [a] → Maybe a, which returns Nothing if the list is empty.
4. Define a function pos such that pos x xs looks for x in xs and returns its position.
For example, find 'a' "abc" yields 0, and find 'a' "bac" yields 1.
(a) Let the type be pos :: Eq a ⇒ a → [a] → Int. In your definition, what happens if x is not in the list?
(b) Let the type be pos :: Eq a ⇒ a → [a] → Maybe Int, such that pos x xs returns Nothing if x is not in the list.
5. Define myConcat :: [[a]] → [a] such that, for example myConcat [[1,2,3], [], [4], [5,6]] = [1,2,3,4,5,6]. Hint: use (++).
6. Define double :: [a] → [a] such that, for example, double [1,2,3] = [1,1,2,2,3,3].
7. Define interleave :: [a] → [a] → [a] such that, for example, interleave [1,2,3,4] [5,6,7] = [1,5,2,6,3,7,4].
8. Define splitLR :: [Either a b] → ([a], [b]) such that, for example:
splitLR [Left 1, Left 3, Right 'a', Left 2, Right 'b'] = ([1,3,2], "ab") .
9. Define a function fan :: a → [a] → [[a]] such that fan x xs inserts x into the 0th, 1st. . . nth positions of xs, where n is the length of xs. For example:
fan 5 [1,2,3,4] = [[5,1,2,3,4], [1,5,2,3,4], [1,2,5,3,4], [1,2,3,5,4], [1,2,3,4,5]] .
10. Define perms :: [a] → [[a]] that returns all permutations of the input list. For example:
perms [1,2,3] = [[1,2,3], [2,1,3], [2,3,1], [1,3,2], [3,1,2], [3,2,1]] .
11. Try to define functions inits and tails yourself, and make sure you understand them. Recall that inits [1,2,3] = [[ ], [1], [1,2], [1,2,3]], and tails [1,2,3] = [[1,2,3], [2,3], [3], [ ]].
我作的參考解答(有給老師改過了XD,應該沒有問題~)
Here is my answer. (The teacher has saw that so I think it's correct.)
這邊我列出第三部分的題目跟我作的參考答案~ (練習使用 List 跟寫遞回函數!)
但我還是建議你看原版的PDF (有全部的題目) 會比較清楚XD
When I am studying in the FLOLAC, the teacher gave us some Haskell exercises to practice.
I put the problems and my answer of section 3 to this article! ( for List and recursive functions.)
But I think you should see the original practicals PDF, which is more clear.
練習題目 Practicals: 3 Inductively Defined Functions on Lists
1. Define a function fstEven :: [Int] → Int that returns the first even number of the input list.
2. Define a function hasZero :: [Int] → Bool that returns True if and only if there is a 0 in the input list.
3. Define a function myLast that takes a list and returns the last (rightmost) element.
(a) Let the type be myLast :: [a] → a. Define myLast.
(b) What happens in the previous definition of the input list is empty?
(c) Define myLast :: [a] → Maybe a, which returns Nothing if the list is empty.
4. Define a function pos such that pos x xs looks for x in xs and returns its position.
For example, find 'a' "abc" yields 0, and find 'a' "bac" yields 1.
(a) Let the type be pos :: Eq a ⇒ a → [a] → Int. In your definition, what happens if x is not in the list?
(b) Let the type be pos :: Eq a ⇒ a → [a] → Maybe Int, such that pos x xs returns Nothing if x is not in the list.
5. Define myConcat :: [[a]] → [a] such that, for example myConcat [[1,2,3], [], [4], [5,6]] = [1,2,3,4,5,6]. Hint: use (++).
6. Define double :: [a] → [a] such that, for example, double [1,2,3] = [1,1,2,2,3,3].
7. Define interleave :: [a] → [a] → [a] such that, for example, interleave [1,2,3,4] [5,6,7] = [1,5,2,6,3,7,4].
8. Define splitLR :: [Either a b] → ([a], [b]) such that, for example:
splitLR [Left 1, Left 3, Right 'a', Left 2, Right 'b'] = ([1,3,2], "ab") .
9. Define a function fan :: a → [a] → [[a]] such that fan x xs inserts x into the 0th, 1st. . . nth positions of xs, where n is the length of xs. For example:
fan 5 [1,2,3,4] = [[5,1,2,3,4], [1,5,2,3,4], [1,2,5,3,4], [1,2,3,5,4], [1,2,3,4,5]] .
10. Define perms :: [a] → [[a]] that returns all permutations of the input list. For example:
perms [1,2,3] = [[1,2,3], [2,1,3], [2,3,1], [1,3,2], [3,1,2], [3,2,1]] .
11. Try to define functions inits and tails yourself, and make sure you understand them. Recall that inits [1,2,3] = [[ ], [1], [1,2], [1,2,3]], and tails [1,2,3] = [[1,2,3], [2,3], [3], [ ]].
我作的參考解答(有給老師改過了XD,應該沒有問題~)
Here is my answer. (The teacher has saw that so I think it's correct.)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
------------practice 3-------------------------------------- | |
--author: Yao-Jen Chang | |
--Email: autek.roy@gmail.com | |
--purpose: answers for practicals section 3 in FLOLAC 2014 | |
--language: haskell | |
--Date: 07/02/2014 | |
------------------------------------------------------------ | |
--1 先不考慮失敗的input | |
fstEven :: [Int] -> Int | |
fstEven (x : xs) = if even x then x else fstEven xs | |
--2 | |
hasZero :: [Int] -> Bool | |
hasZero [] = False | |
hasZero (x :xs) = if x ==0 then True else hasZero xs | |
--3 (a) | |
myLast :: [a] -> a --any type will work | |
myLast[x] = x | |
myLast(x :xs) = myLast xs | |
--3 (b) it will crash | |
--3 (c) | |
myLast2 :: [a] -> Maybe a --any type will work | |
myLast2[]=Nothing | |
myLast2[x] = Just x | |
myLast2(x :xs) = myLast2 xs | |
--4 | |
pos :: Eq a => a -> [a] -> Int | |
pos y (x: xs) = if x==y then 0 else | |
case pos y xs of | |
n -> n + 1 | |
--4 (a) it will crash | |
--4 (b) | |
pos2 :: Eq a => a -> [a] -> Maybe Int | |
pos2 y [] = Nothing | |
pos2 y (x: xs) = if x==y then Just 0 else | |
case pos2 y xs of | |
Nothing -> Nothing | |
Just n -> Just (n + 1) | |
--5 | |
--test input: myConcat [[12, 4], [], [4], [65,9,3], [7]] | |
myConcat :: [[a]] -> [a] | |
myConcat[] = [] | |
myConcat (x :xs) = x ++ myConcat xs | |
--6 | |
double::[a] -> [a] | |
double[] = [] | |
double (x: xs) = x : x : double xs | |
--7 | |
--test input: interleave [1, 2, 3, 4][5, 6, 7, 8] | |
interleave :: [a] -> [a] -> [a] | |
interleave [] x = x | |
interleave x [] = x | |
interleave (x:xs) (y:ys) = x : y : interleave xs ys | |
--same as above | |
--interleave2 [5,6,7] [2,3,4] = [5,2,6,3,7,4] | |
--interleave2 (1:[2,3,4]) [5,6,7] = 1 : interleave2 [5,6,7] [2,3,4] | |
interleave2 :: [a] -> [a] -> [a] | |
interleave2 [] x = x | |
interleave2 (x:xs) ys = x : interleave2 ys xs | |
--8 | |
--test input: splitLR [Left 1, Left 3, Right 'a', Left 2, Right 'b'] = ([1, 3, 2], "ab") | |
splitLR ::[Either a b] -> ([a], [b]) | |
splitLR [] = ([], []) | |
splitLR (x:xs) = case x of | |
Left a -> (a:fst(rs), snd(rs)) | |
Right a -> (fst(rs), a:snd(rs)) | |
where rs = (splitLR xs) | |
--9 | |
--fan 5 [2, 3] = [[5, 2, 3], [2, 5, 3], [2, 3, 5]] | |
--fan 5 [1, 2, 3] = [[5,1,2,3],[1,5,2,3],[1,2,5,3],[1,2,3,5]] | |
--test input: fan 5 [1, 2, 3, 4] | |
fan :: a -> [a] -> [[a]] | |
fan y [] = [[y]] | |
fan y (x: xs) = (y:x:xs): map (x:) rs --map example: map (9:)[[1,2], [4, 5]] -> [[9,1,2],[9,4,5]] | |
where rs = (fan y xs) | |
--10 | |
--perms [3] = [[3]] | |
--perms [2, 3] = [[2, 3], [3, 2]] | |
--perms [1, 2, 3] = [[1, 2, 3], [2, 1, 3], [2, 3, 1], [1, 3, 2], [3, 1, 2]] | |
--perms (1:[2, 3]) = [[2, 3], [3, 2]] | |
--test input: perms [1,2,3] | |
perms :: [a] -> [[a]] | |
perms [x] = [[x]] | |
perms (x:xs) = myConcat rs | |
where rs = map (fan x) (perms xs) | |
--11 寫完之後,發現跟講義一模一樣XD | |
--inits [2, 3] = [[], [2], [2, 3]] | |
--inits [1, 2, 3] = [[],[1],[1,2],[1,2,3]] | |
inits :: [a] -> [[a]] --all prefixes | |
inits [] = [[]] | |
inits (x:xs) = []: (map (x:) (inits xs))--map example: map (9:)[[1,2], [4, 5]] -> [[9,1,2],[9,4,5]] | |
--tails [2, 3] =[[2,3],[3],[]] | |
--tails [1, 2, 3] = [[1,2,3],[2,3],[3],[]] | |
tails :: [a] -> [[a]] --all suffixes | |
tails [] = [[]] | |
tails (x:xs) = (x:xs) : tails xs -- (x:xs) means the original element |
推薦相關文章給您:
1. 安裝Haskell環境與基本操作教學 - 使用GHCi
2. Haskell Practicals 1 - Functions 函數
3. Haskell Practicals 2 - Products and Sums 乘積和合
4. Haskell Practicals 3 - Lists and Recursive Function 遞回函數
If you want to use (copy, paste or quote) my original article, please contact me through email. (autek.roy@gmail.com) If there is any mistake or comment, please let me know. :D
如要使用(複製貼上或轉載)作者原創文章, 請來信跟我聯絡。(autek.roy@gmail.com) 如果有發現任何的錯誤與建議請留言或跟我連絡。 : )
沒有留言:
張貼留言
請留下您的任何想法或建議!
Please leave any thought or comment!