Skip to content

Instantly share code, notes, and snippets.

@ikedaisuke
Last active October 26, 2017 21:38
Show Gist options
  • Save ikedaisuke/11a5ea3a560a607aca19 to your computer and use it in GitHub Desktop.
Save ikedaisuke/11a5ea3a560a607aca19 to your computer and use it in GitHub Desktop.
simple uniq in Idris
-- % 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
-- % 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
@ikedaisuke
Copy link
Author

case foo of
True => ...
False => ...

if foo
then ...
else ...
では、termination checker が異なる振る舞いをする
case 文では maybe terminate だが if 文では terminate を理解するらしい

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment