Skip to content

Instantly share code, notes, and snippets.

@LouiS0616
Created April 15, 2020 06:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LouiS0616/588adc638b18ba769d44a73becc793c6 to your computer and use it in GitHub Desktop.
Save LouiS0616/588adc638b18ba769d44a73becc793c6 to your computer and use it in GitHub Desktop.
連続するスペースの削除いろいろ
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