在上 FLOLAC 的課程時,老師出了一些 Haskell 的練習題給我們練習。
這邊我列出第二部分的題目跟我作的參考答案~
但我還是建議你看原版的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.
let x = ((1,’a’), True)
This defines a new symbol x, with value ((1, 'a'),True).
(a) Find out the type of x by a GHCi command.
(b) How do you extract the 1 in x? Type an expression . . . x into GHCi such that the result is 1.
(c) Try to extract 'a' and True from x too.
2. Define a function swap :: (a, b) → (b, a) that, as the name and type suggests, swaps the components
(a) Define swap using pattern matching: swap (x, y) = . . ..
(b) Define swap using fst and snd: swap x = . . ..
(c) Define swap using case.
3. Define a function half :: Int → Either Int Int such that
• if n is even, half n returns Left k with 2 × k = n;
• if n is odd, half n returns Right k with 2 × k + 1 = n.
You may use the function div. Find out what it does by yourself.
4. What are the types of the following expressions?
(a) λx → (snd x, fst x).
(b) λf x → f x x.
(c) Define:
myEither f g x = case x of
Left y → f y
Right z → g z .
What is the type of myEither?
(d) λf x y → f (fst y) x.
(e) λf x y → fst (f y x).
(f) λx y → x.
(g) λf g x → f x (g x).
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
這是我的答案 Here is my answer!
推薦相關文章給您:
1. 安裝Haskell環境與基本操作教學 - 使用GHCi
2. Haskell Practicals 1 - Functions 函數
3. Haskell Practicals 2 - Products and Sums 乘積和合
4. Haskell Practicals 3 - Lists and Recursive Function 遞回函數
如要使用(複製貼上或轉載)作者原創文章, 請來信跟我聯絡。(autek.roy@gmail.com) 如果有發現任何的錯誤與建議請留言或跟我連絡。 : )
這邊我列出第二部分的題目跟我作的參考答案~
但我還是建議你看原版的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: 2 Products and Sums
1. In GHCi, issue the commandlet x = ((1,’a’), True)
This defines a new symbol x, with value ((1, 'a'),True).
(a) Find out the type of x by a GHCi command.
(b) How do you extract the 1 in x? Type an expression . . . x into GHCi such that the result is 1.
(c) Try to extract 'a' and True from x too.
2. Define a function swap :: (a, b) → (b, a) that, as the name and type suggests, swaps the components
(a) Define swap using pattern matching: swap (x, y) = . . ..
(b) Define swap using fst and snd: swap x = . . ..
(c) Define swap using case.
3. Define a function half :: Int → Either Int Int such that
• if n is even, half n returns Left k with 2 × k = n;
• if n is odd, half n returns Right k with 2 × k + 1 = n.
You may use the function div. Find out what it does by yourself.
4. What are the types of the following expressions?
(a) λx → (snd x, fst x).
(b) λf x → f x x.
(c) Define:
myEither f g x = case x of
Left y → f y
Right z → g z .
What is the type of myEither?
(d) λf x y → f (fst y) x.
(e) λf x y → fst (f y x).
(f) λx y → x.
(g) λf g x → f x (g x).
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
這是我的答案 Here is my answer!
推薦相關文章給您:
1. 安裝Haskell環境與基本操作教學 - 使用GHCi
2. Haskell Practicals 1 - Functions 函數
3. Haskell Practicals 2 - Products and Sums 乘積和合
4. Haskell Practicals 3 - Lists and Recursive Function 遞回函數
如要使用(複製貼上或轉載)作者原創文章, 請來信跟我聯絡。(autek.roy@gmail.com) 如果有發現任何的錯誤與建議請留言或跟我連絡。 : )
沒有留言:
張貼留言
請留下您的任何想法或建議!
Please leave any thought or comment!