Last active
October 26, 2017 21:38
-
-
Save ikedaisuke/11a5ea3a560a607aca19 to your computer and use it in GitHub Desktop.
simple uniq in Idris
This file contains hidden or 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
-- % idris simple-uniq.idr -o uniq | |
module Main | |
getContents : IO String | |
getContents = go "" | |
where go : String -> IO String | |
go s = do case !(feof stdin) of | |
True => return s | |
False => do t <- fread stdin | |
go (s ++ t) | |
interact : (String -> String) -> IO () | |
interact f = do s <- getContents | |
putStr (f s) | |
putStr "\n" | |
uniq : String -> String | |
uniq = unlines . go . lines | |
where go : List String -> List String | |
go xs = case xs of | |
Nil => Nil | |
x :: Nil => x :: Nil | |
y :: (z :: zs) => | |
case y == z of | |
True => go (z :: zs) | |
False => y :: go (z :: zs) | |
main : IO () | |
main = interact uniq |
This file contains hidden or 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
-- % idris terminated-uniq.idr -o uniq | |
module Main | |
getContents : IO String | |
getContents = go "" | |
where go : String -> IO String | |
go s = do case !(feof stdin) of | |
True => return s | |
False => do t <- fread stdin | |
go (s ++ t) | |
interact : (String -> String) -> IO () | |
interact f = do s <- getContents | |
putStr (f s) | |
putStr "\n" | |
uniq : String -> String | |
uniq = unlines . go . lines | |
where go : List String -> List String | |
go xs = case xs of | |
Nil => Nil | |
x :: Nil => x :: Nil | |
y :: (z :: zs) => | |
if y == z | |
then go (z :: zs) | |
else y :: go (z :: zs) | |
main : IO () | |
main = interact uniq |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
case foo of
True => ...
False => ...
と
if foo
then ...
else ...
では、termination checker が異なる振る舞いをする
case 文では maybe terminate だが if 文では terminate を理解するらしい