在上 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.
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) 如果有發現任何的錯誤與建議請留言或跟我連絡。 : )
這邊我列出第一部分的題目跟我作的參考答案~ (練習使用函數!)
但我還是建議你看原版的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!
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 1--------------------- | |
--author: Yao-Jen Chang | |
--Email: autek.roy@gmail.com | |
--purpose: answers for practicals section 1 in FLOLAC 2014 | |
--language: haskell | |
--Date: 06/30/2014 | |
------------------------------------------------------------ | |
--1 | |
isEven :: Int -> Bool | |
isEven x = (mod x 2)==0 | |
--2 | |
--不會Int 轉 Double...所以直接在型別宣告時用Double了 | |
computeCir :: Double -> Double | |
computeCir x = x * x * (22/7) | |
--3 | |
smaller ::Int->Int->Int | |
smaller x y=if x<= y then x else y | |
str3 = smaller 3 | |
{----or you may use this---- | |
smaller x y|x <=y = x | |
|x > y = y | |
-} | |
--4 | |
square ::Int->Int | |
square x = x * x | |
twice ::(a -> a) -> (a -> a) | |
twice f x = f (f x) | |
quad ::Int ->Int | |
--quad x = x * x * x * x | |
--quad x = square (square x) --reduce from the right | |
quad x = twice square x | |
--5 | |
------it's same to twice-------------- | |
twice_2 ::(a -> a) -> (a -> a) | |
twice_2 f = f . f | |
quad_2 ::Int ->Int | |
quad_2 x = twice_2 square x | |
------just for practice, it's not in the PDF's problems---- | |
infinity :: Int -> Int | |
infinity x = x + 1 | |
sum2 :: [Int] -> Int | |
sum2[] = 0 | |
sum2 (x: xs) = x + sum2 xs | |
{-------how to define local variable------ | |
f ::(Float, Float) -> Float | |
f (x, y) = let a = (x + y)/2 | |
b = (x + y)/3 | |
in (a + 1) * (b + 2) | |
-------------------------------------} | |
--------------------------------------------------------- | |
推薦相關文章給您:
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!