Skip to content

Instantly share code, notes, and snippets.

@tan-yuki
Created May 21, 2017 21:45
Show Gist options
  • Save tan-yuki/ac597f622f9c7be7cc601b0b2bf609af to your computer and use it in GitHub Desktop.
Save tan-yuki/ac597f622f9c7be7cc601b0b2bf609af to your computer and use it in GitHub Desktop.
length' :: String -> Int
length' = foldl (\ x _ -> x + 1) 0
last' :: Show a => [a] -> (Maybe a)
last' = foldl (\ _ x -> Just x) Nothing
head' :: Show a => [a] -> (Maybe a)
head' [] = Nothing
head' (x:_) = Just x
tail' :: Show a => [a] -> (Maybe [a])
tail' [] = Nothing
tail' (_:xs) = Just xs
substring' :: Show a => Int -> Int -> [a] -> (Maybe [a])
substring' _ _ [] = Nothing
substring' start end xs
| start > end = Nothing
| (length xs) <= start = Nothing
| otherwise = Just $ take (end - start) $ drop start xs
main :: IO ()
main = do
-- length' test
print "length' test =========="
print $ length' "hogehoge"
print $ length' "fuga"
print $ length' ""
-- last' test
print "last' test =========="
print $ last' [1, 2, 3]
print $ last' "abcdef"
print $ last' ""
print $ last' ([]::[Int])
-- head' test
print "head' test =========="
print $ head' [1, 2, 3]
print $ head' "abcdef"
print $ head' ""
print $ head' ([]::[Int])
-- tail' test
print "tail' test =========="
print $ tail' [1, 2, 3]
print $ tail' "abcdef"
print $ tail' ""
print $ tail' ([]::[Int])
-- substring' test
print "substring' test =========="
print $ substring' 0 2 [1, 2, 3]
print $ substring' 1 3 "abcdef"
print $ substring' 1 5 [1, 2, 3]
print $ substring' 4 5 [1, 2, 3]
print $ substring' 2 1 [1, 2, 3]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment