Skip to content

Instantly share code, notes, and snippets.

@evmorov
Created October 24, 2016 21:42
Show Gist options
  • Save evmorov/3bdbd33fe3fddb5d3c26364bd933d208 to your computer and use it in GitHub Desktop.
Save evmorov/3bdbd33fe3fddb5d3c26364bd933d208 to your computer and use it in GitHub Desktop.
main = do
print $ head1 ['x', 'y', 'z']
print $ max1 1 2
print $ fst1 (6, 5)
print $ zip1 [1, 2, 3, 4] ['a', 'b']
print $ filter1 (< 5) [1, 2, 8, 9]
print $ filter2 (\x -> x < 5) [1, 2, 8, 9]
print $ length1 [1, 2, 3]
print $ length2 [1, 2, 3]
print $ length3 [1, 2, 3]
print $ length4 [1, 2, 3]
head1 :: [a] -> a
head1 (x:xs) = x
max1 :: Ord a => a -> a -> a
max1 x y
| x > y = x
| otherwise = y
fst1 :: (a, b) -> a
fst1 (x, y) = x
zip1 :: [a] -> [b] -> [(a, b)]
zip1 [] ys = []
zip1 xs [] = []
zip1 (x:xs) (y:ys) = (x, y) : zip1 xs ys
filter1 :: (a -> Bool) -> [a] -> [a]
filter1 pred lst
| null lst = []
| otherwise =
if pred x
then x : filter1 pred xs
else filter1 pred xs
where
x:xs = lst
filter2 pred [] = []
filter2 pred (x:xs)
| pred x = x : filter2 pred xs
| otherwise = filter2 pred xs
length1 :: [a] -> Int
length1 [] = 0
length1 (x:xs) = 1 + length1 xs
length2 lst =
if lst == []
then 0
else let x:xs = lst
in 1 + length2 xs
length3 lst
| lst == [] = 0
| otherwise =
let x:xs = lst
in 1 + length3 xs
length4 = _length4
where
_length4 [] = 0
_length4 (x:xs') = 1 + _length4 xs'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment