Skip to content

Instantly share code, notes, and snippets.

@SeungUkLee
Last active October 29, 2017 18:20
Show Gist options
  • Save SeungUkLee/a9b45c09976cfe88137f8dad95321721 to your computer and use it in GitHub Desktop.
Save SeungUkLee/a9b45c09976cfe88137f8dad95321721 to your computer and use it in GitHub Desktop.
-- likelion
-- haskell 함수를 구현
-- fst
fst' :: (a,b) -> a
fst' (a, _) = a
-- snd
snd' :: (a,b) -> b
snd' (_, b) = b
-- succ
-- succ' :: a -> a
succ' a = a + 1
-- min
min' :: (Ord a) => a -> a -> a
min' a b
| a > b = b
| otherwise = a
-- max
max' :: (Ord a) => a -> a -> a
max' a b
| a > b = a
| otherwise = b
-- &&
True &&& b = b
False &&& _ = False
-- ||
True ||| _ = True
False ||| b = b
-- ==
(===) a b | a == b = True
| otherwise = False
-- /=
(/==) a b | a == b = False
| otherwise = True
-- ++
(+++) :: [a] -> [a] -> [a]
[] +++ ys = ys
(x:xs) +++ ys = x : (xs +++ ys)
-- !!
-- >
(>!) a b | a > b = True
| otherwise = False
-- <
(<!) a b | a < b = True
| otherwise = False
-- head
head' :: [a] -> a
head' (x:xs) = x
-- tail
tail' :: [a] -> [a]
tail' (x:xs) = xs
-- last
last' :: [a] -> a
last' [x] = x
last' (x:xs) = last' xs
-- init
init' :: [a] -> [a]
init' [_] = []
init' (x:xs) = x : init' xs
-- length
length' [] = 0
length' (x:xs) = 1 + length' xs
-- null
-- reverse
reverse' (x:xs) = last (x:xs) : reverse (init (x:xs))
-- take
take' :: Int -> [a] -> [a]
take' 0 xs = []
take' _ [] = []
take' n (x:xs) = x : take' (n-1) xs
-- drop
drop' :: Int -> [a] -> [a]
drop' 0 xs = xs
drop' _ [] = []
drop' n (x:xs) = drop' (n-1) xs
-- maximum
maximum' :: (Ord a, Foldable t) => t a -> a
maximum' = foldl1 max
-- mininum
mininum' :: (Ord a, Foldable t) => t a -> a
mininum' = foldl1 min
-- sum
sum' [] = 0
sum' (x:xs) = x + sum' xs
-- product
product' [] = 0
product' (x:xs) = x * product' xs
-- elem
elem' :: Eq a => a -> [a] -> Bool
elem' n [] = False
elem' n (x:xs) = if n==x then True else elem' n xs
-- zip
zip' :: [a] -> [b] -> [(a,b)]
zip' [] _ = []
zip' _ [] = []
zip' (x:xs) (y:ys) = (x,y) : zip' xs ys
-- quicksort
f [] = []
f (x:xs) = f smaller ++ [x] ++ larger
where
smaller = [a | a <- xs, a <= x]
larger = [b | b <- xs, b > x]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment