有些內容使用中英雙語,有些只有英文或中文。歡迎使用與分享任何內容,但先來信告知並標示此部落格為出處。
Some parts use both Chinese and English, but some parts use only one language. Feel free to share, but please contact me first and list this blog as your reference.

2014年7月6日 星期日

[Haskell] FLOLAC - Functional Programming Practicals 3 - Lists and Recursive Function 遞回函數

在上 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.

練習題目 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.)

推薦相關文章給您:
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!