有些內容使用中英雙語,有些只有英文或中文。歡迎使用與分享任何內容,但先來信告知並標示此部落格為出處。
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月7日 星期一

[Haskell] FLOLAC - Functional Programming Practicals 1 - Functions 函數

在上 FLOLAC 的課程時,老師出了一些 Haskell 的練習題給我們練習。
這邊我列出第一部分的題目跟我作的參考答案~ (練習使用函數!)
但我還是建議你看原版的PDF (有全部的題目) 會比較清楚XD
BTW, 目前我跳過第六題了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: 1 Functions

1. Define a function even :: Int → Bool that determines whether the input is an even number. You may use the following functions:
mod :: Int → Int → Int ,
(==) :: Int → Int → Bool .
(Types of the functions written above are not in their most general form.)

2. Define a function that computes the area of a circle with given radius r (using 22/7 as an approximation
to π). The return type of the function might be Double.

3. Type in the definition of smaller into your working file. Then try the following:
(a) In GHCi, type :t smaller to see the type of smaller.
(b) Try applying it to some arguments, e.g. smaller 3 4, smaller 3 1.
(c) In your working file, define a new function st3 = smaller 3.
(d) Find out the type of st3 in GHCi. Try st3 4, st3 1. Explain the results you see.

4. Type in the definition of square in your working file.
(a) Define a function quad :: Int → Int such that quad x computes x^4.
(b) Type in this definition into your working file. Describe, in words, what this function does.
twice :: (a → a) → (a → a)
twice f x = f (f x) .
(c) Define quad using twice.

5. Replace the previous twice with this definition:
twice :: (a → a) → (a → a)
twice f = f · f .
(a) Does quad still behave the same?
(b) Explain in words what this operator (·) does.

6. Let the following identifiers have type:
f :: Int → Char
g :: Int → Char → Int
h :: (Char → Int) → Int → Int
x :: Int
y :: Int
c :: Char
Which of the following expressions are type correct?
1. (g · f) x c
2. (g x · f) y
3. (h · g) x y
4. (h · g x) c
5. h · g x c

You may type the expressions into Haskell and see whether they type check. To define f, for example,
include the following in your working file:
f :: Int → Char
f = undefined
However, it is better if you can explain why the answers are as they are.



這是我的答案 Here is my answers!

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