Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Find the K'th element of a list. The first element in the list is
- -- number 1.
- main :: IO ()
- main = mapM_ (putStr . (\x -> if x == "eggs" then x ++ ";\n" else x ++ ", ")) $
- [ one
- , two
- , three
- , four
- , five
- , six
- , seven
- , eight
- , nine
- , ten
- , eleven
- , twelve
- , thirteen
- , fourteen
- , fifteen
- , sixteen
- , seventeen] <*> [1 .. 6] <*> [lst]
- lst :: [String]
- lst = ["foo", "bar", "baz", "ham", "spam", "eggs"]
- one :: Int -> [a] -> a
- one n xs = xs !! (n - 1)
- two :: Int -> [a] -> a
- two n xs = head $ drop (n - 1) xs
- three :: Int -> [a] -> a
- three n xs = last $ take n xs
- four :: Int -> [a] -> a
- four n xs = reverse xs !! (length xs - n)
- five :: Int -> [a] -> a
- five n xs = head $ drop (length xs - n) (reverse xs)
- six :: Int -> [a] -> a
- six n xs = last $ take (length xs + 1 - n) (reverse xs)
- seven :: Int -> [a] -> a
- seven n xs = last $ fst $ splitAt n xs
- eight :: Int -> [a] -> a
- eight n xs = head $ snd $ splitAt (n - 1) xs
- nine :: Int -> [a] -> a
- nine = aux 0
- where
- aux l n (y:ys)
- | l == (n - 1) = y
- | otherwise = aux (l + 1) n ys
- ten :: Int -> [a] -> a
- ten n xs = snd $
- head $
- filter (\t -> n == fst t) $ zip [1..] xs
- eleven :: Int -> [a] -> a
- eleven n xs = snd $
- foldl1 (\(f, s) (f1, s1) -> if f == n then (f, s) else (f1, s1)) $
- zip [1..] xs
- twelve :: Int -> [a] -> a
- twelve n xs = snd $
- head $
- zip [1..] xs >>= (\(f, s) -> [(f, s) | f == n])
- thirteen :: Int -> [a] -> a
- thirteen n xs = snd $ last $ zip [1 .. n] xs
- fourteen :: Int -> [a] -> a
- fourteen n xs = head [snd x | x <- zip [1..] xs, fst x == n]
- fifteen :: Int -> [a] -> a
- fifteen n (x:xs) = if n == 1 then x else fifteen (n - 1) xs
- sixteen :: Int -> [a] -> a
- sixteen n xs = snd $
- head $
- scanr1 (\(f, s) (f1, s1) -> if f == n then (f, s) else (f1, s1)) $
- zip [1..] xs
- seventeen :: Int -> [a] -> a
- seventeen n xs = aux (length xs) n xs
- where
- aux ll n (y:ys) = if ll - length ys == n then y else aux ll n ys
- -- foo, bar, baz, ham, spam, eggs;
- -- foo, bar, baz, ham, spam, eggs;
- -- foo, bar, baz, ham, spam, eggs;
- -- foo, bar, baz, ham, spam, eggs;
- -- foo, bar, baz, ham, spam, eggs;
- -- foo, bar, baz, ham, spam, eggs;
- -- foo, bar, baz, ham, spam, eggs;
- -- foo, bar, baz, ham, spam, eggs;
- -- foo, bar, baz, ham, spam, eggs;
- -- foo, bar, baz, ham, spam, eggs;
- -- foo, bar, baz, ham, spam, eggs;
- -- foo, bar, baz, ham, spam, eggs;
- -- foo, bar, baz, ham, spam, eggs;
- -- foo, bar, baz, ham, spam, eggs;
- -- foo, bar, baz, ham, spam, eggs;
- -- foo, bar, baz, ham, spam, eggs;
- -- foo, bar, baz, ham, spam, eggs;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement