Created
July 12, 2010 21:55
-
-
Save Koitaro/473125 to your computer and use it in GitHub Desktop.
I/O library for Project Euler
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
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] |
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
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