Created
April 15, 2020 06:05
-
-
Save LouiS0616/588adc638b18ba769d44a73becc793c6 to your computer and use it in GitHub Desktop.
連続するスペースの削除いろいろ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.List (group) | |
removeSequencialSpace1 :: String -> String | |
removeSequencialSpace1 = concat . compressSpaces . group | |
where | |
compressSpaces :: [String] -> [String] | |
compressSpaces = foldr (\str acc -> (if ' ' `elem` str then " " else str): acc) [] | |
removeSequencialSpace2 :: String -> String | |
removeSequencialSpace2 src = loop src False | |
where | |
loop :: String -> Bool -> String | |
loop [] _ = [] | |
loop (' ':xs) True = loop xs True | |
loop ( x:xs) _ = x: loop xs (x == ' ') | |
removeSequencialSpace3 :: String -> String | |
removeSequencialSpace3 = foldr loop "" | |
where | |
loop :: Char -> String -> String | |
loop x "" = [x] | |
loop ' ' acc@(' ':_) = acc | |
loop x acc = x: acc | |
removeSequencialSpace4 :: String -> String | |
removeSequencialSpace4 (' ':' ':ys) = removeSequencialSpace4 (' ':ys) | |
removeSequencialSpace4 ( x: y:ys) = x: removeSequencialSpace4 ( y:ys) | |
removeSequencialSpace4 ys = ys | |
main :: IO() | |
main = interact removeSequencialSpace4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment