Created
May 26, 2018 16:16
-
-
Save YuRen-tw/3e2c0fa9f4a36ac22d0da2f041e6676f to your computer and use it in GitHub Desktop.
FLOLAC 2018 0-p: Prerequisites
This file contains hidden or 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
| {- 1 -} | |
| myFst :: (a, b) -> a | |
| myFst (a, b) = a | |
| {- 2 -} | |
| myOdd :: Int -> Bool | |
| myOdd = (== 1) . flip mod 2 | |
| {- 3 -} | |
| {- | |
| (a) Ord 是一個 Typeclass,它規範某個 type 必須要有(全)序關係(有 <=)。 | |
| qs 的 type 表示它是一個函數,從 list of type a 到 list of type a,其中 type a 屬於 Ord。 | |
| (b) (++) 的 type 是 [a] -> [a] -> [a]。 | |
| (++) 的功能是將兩個 list 串接起來。 | |
| (c) ys 的元素是 xs 中小於等於 x 的元素; | |
| zs 的元素是 xs 中大於 x 的元素。 | |
| (d) qs 的功能是用遞迴的方法對一個 list 做 Quicksort。 | |
| -} | |
| {- 3(e) -} | |
| qs' :: Ord a => [a] -> [a] | |
| qs' ws = case ws of [] -> [] | |
| (x:xs) -> let ys = [ y | y <- xs, y <= x ] | |
| zs = [ z | z <- xs, x < z ] | |
| in qs' ys ++ [x] ++ qs' zs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Excellent!