Skip to content

Instantly share code, notes, and snippets.

@Syzygies
Created October 28, 2012 19:19
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 Syzygies/3969553 to your computer and use it in GitHub Desktop.
Save Syzygies/3969553 to your computer and use it in GitHub Desktop.
module Main where
import qualified Data.ByteString.Char8 as B
import Data.Char (chr,ord)
isSpace ∷ Char → Bool
isSpace ' ' = True
isSpace '\t' = True
isSpace '\n' = True
isSpace '\r' = True
isSpace _ = False
toUpper ∷ Char → Char
toUpper x
| (x >= 'a' && x <= 'z') = chr $ ord x - 32
| otherwise = x
fun ∷ Char → Char → Char
fun a b | isSpace a = toUpper b
| otherwise = b
convert ∷ B.ByteString → B.ByteString
convert = B.tail . B.scanl fun ' '
main ∷ IO ()
main = B.readFile "input.txt" >>= B.putStr . convert
module Main where
import Data.Char (chr,ord)
import Foreign.C.Types (CInt(..))
import qualified Data.ByteString.Char8 as B
foreign import ccall unsafe "u_iswspace"
iswspace ∷ CInt → CInt
isSpace ∷ Char → Bool
{-# INLINE isSpace #-}
isSpace c
| c > '\x20' && c < '\xa0' = False
| c == ' ' = True
| '\t' <= c && c <= '\r' = True
| c == '\xa0' = True
| otherwise = iswspace (fromIntegral (ord c)) /= 0
toUpper ∷ Char → Char
toUpper x
| (x >= 'a' && x <= 'z') = chr $ ord x - 32
| otherwise = x
fun ∷ Char → Char → Char
fun a b | isSpace a = toUpper b
| otherwise = b
convert ∷ B.ByteString → B.ByteString
convert = B.tail . B.scanl fun ' '
main ∷ IO ()
main = B.readFile "input.txt" >>= B.putStr . convert
module Main where
import Data.Char (chr,ord)
import Foreign.C.Types (CInt(..))
import qualified Data.ByteString.Char8 as B
foreign import ccall unsafe "u_iswspace"
iswspace ∷ CInt → CInt
isSpace ∷ Char → Bool
{-# INLINE isSpace #-}
isSpace c
| c > '\x20' && c < '\xa0' = False
| c == ' ' = True
| '\t' <= c && c <= '\r' = True
| c == '\xa0' = True
| otherwise = iswspace (fromIntegral (ord c)) /= 0
toUpper ∷ Char → Char
toUpper x
| (x >= 'a' && x <= 'z') = chr $ ord x - 32
| otherwise = x
fun ∷ Char → Char → Char
fun a b | isSpace a = toUpper b
| otherwise = b
op ∷ Bool → Char → (Bool, Char)
op flag c = if isSpace c then (True,c) else (False,d)
where d = if flag then toUpper c else c
main ∷ IO ()
main = B.readFile "input.txt" >>= B.putStr . snd . B.mapAccumL op True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment