Skip to content

Instantly share code, notes, and snippets.

@redbug312
Created May 21, 2018 16:14
Show Gist options
  • Save redbug312/01fc304e10d07e9a62677502e6a7f571 to your computer and use it in GitHub Desktop.
Save redbug312/01fc304e10d07e9a62677502e6a7f571 to your computer and use it in GitHub Desktop.
Prerequisites for signing up FLOLAC'18
-- p1
myFst :: (a, b) -> a
myFst = fst -- seemed the only way :(
-- p2
myOdd :: Int -> Bool
myOdd = (==) 1 . flip mod 2
-- p3
{-
(a) 1. Ord 是被用於具有全序關係 datatype 的 class
2. qs 為輸入 list of a 後輸出 list of a 的函數,
其中 a 是任何 constituent type 包含 Ord class 的 datatype
(b) 1. (++) :: [a] -> [a] -> [a]
2. 將兩個 lists 以呼叫順序前後串接在一起
(c) ys 是傳入的 list 中,僅留有小於等於其首項(不包含該首項)的 list
zs 是傳入的 list 中,僅留有大於其首項的 list
(d) 由於 < 被定義參考到 <= 和 ==。qs 會以快速排序演算法(非原地),
重新排列傳入 list 使得任何元素 a 與排列其後的元素 b 皆有 a <= b
(e) 如下
-}
qs' :: Ord a => [a] -> [a]
qs' xs' = case xs' of [] -> []
x:xs -> let ys = [ y | y <- xs, y <= x ];
zs = [ z | z <- xs, x < z ]
in qs' ys ++ [x] ++ qs' zs
-- main = print $ (myFst (1, 2), myOdd 2, qs' [2,1,4,3,5])
@L-TChen
Copy link

L-TChen commented Jul 3, 2018

Well done!

myFst = fst -- seemed the only way :(

You can do it by pattern matching, i.e.

myFst (x, _) = x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment