Skip to content

Instantly share code, notes, and snippets.

@Koitaro
Created July 12, 2010 21:55
Show Gist options
  • Save Koitaro/473125 to your computer and use it in GitHub Desktop.
Save Koitaro/473125 to your computer and use it in GitHub Desktop.
I/O library for Project Euler
definition module EulerIO
put :: a *World -> *World | <<< a
puts :: [a] *World -> *World | <<< a
reads :: File -> [Char]
readlines :: File -> [String]
splitCharsAt :: Char -> ([Char] -> [[Char]])
splitStringAt :: Char -> String -> [String]
implementation module EulerIO
import StdEnv
put :: a *World -> *World | <<< a
put x world
# (console, world) = stdio world
# console = console <<< x <<< '\n'
= snd (fclose console world)
puts :: [a] *World -> *World | <<< a
puts [] world = world
puts [x:xs] world = puts xs (put x world)
reads :: File -> [Char]
reads file
| not ok = []
| otherwise = [c : reads rest]
where
(ok, c, rest) = sfreadc file
readlines :: File -> [String]
readlines file
| sfend file = []
# (line, file) = sfreadline file
= [trim line : readlines file]
where
trim arr = toString [x \\ x <-: arr | x <> '\n']
splitCharsAt :: Char -> [Char] -> [[Char]]
splitCharsAt c = f [] [] where
f res tmp [] = (reverse o map reverse) [tmp:res]
f res tmp [x:xs] = if (x == c) (f [tmp:res] [] xs) (f res [x:tmp] xs)
splitStringAt :: Char -> String -> [String]
splitStringAt separator = map toString o splitCharsAt separator o fromString
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment