在 Haskell 中条件处理 IO 的惯用方法

发布于 2024-11-02 16:28:46 字数 871 浏览 3 评论 0原文

我正在 Haskell 中编写一个小 shell 脚本,它可以接受可选参数。但是,如果参数不存在,我想从 stdin 获取一行来请求值。

在 Haskell 中执行此操作的惯用方法是什么?

#!/usr/bin/env runhaskell

import Control.Applicative ((<$>))
import Data.Char (toLower)
import IO (hFlush, stdout)
import System.Environment (getArgs)

main :: IO ()
main = do args <- getArgs
          -- here should be some sort of branching logic that reads
          -- the prompt unless `length args == 1`
          name <- lowerCase <$> readPrompt "Gimme arg: "
          putStrLn name

lowerCase = map toLower

flushString :: String -> IO ()
flushString s = putStr s >> hFlush stdout

readPrompt :: String -> IO String
readPrompt prompt = flushString prompt >> getLine

哦,如果有办法使用 Control.ApplicativeControl.Arrow 中的某些内容来实现这一点,我想知道。我对这两个模块非常感兴趣。

谢谢!

I'm writing a little shell script in Haskell which can take an optional argument. However, if the argument is not present, I'd like to get a line from stdin in which to ask for a value.

What would be the idiomatic way to do this in Haskell?

#!/usr/bin/env runhaskell

import Control.Applicative ((<
gt;))
import Data.Char (toLower)
import IO (hFlush, stdout)
import System.Environment (getArgs)

main :: IO ()
main = do args <- getArgs
          -- here should be some sort of branching logic that reads
          -- the prompt unless `length args == 1`
          name <- lowerCase <
gt; readPrompt "Gimme arg: "
          putStrLn name

lowerCase = map toLower

flushString :: String -> IO ()
flushString s = putStr s >> hFlush stdout

readPrompt :: String -> IO String
readPrompt prompt = flushString prompt >> getLine

Oh, and if there's a way to do it with something from Control.Applicative or Control.Arrow I'd like to know. I've become quite keen on these two modules.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹊巢 2024-11-09 16:28:46
main :: IO ()
main = do args <- getArgs
          name <- lowerCase <
gt; case args of
            [arg] -> return arg
            _     -> readPrompt "Gimme arg: "
          putStrLn name
main :: IO ()
main = do args <- getArgs
          name <- lowerCase <
gt; case args of
            [arg] -> return arg
            _     -> readPrompt "Gimme arg: "
          putStrLn name
被你宠の有点坏 2024-11-09 16:28:46

这不适合您的特定用例,但问题标题让我立即想到 Control.Monad 中的 when 。直接来自文档:

当 :: Monad m =>布尔-> m()->;米()

一元表达式的条件执行。

示例:

main = do args <- getArgs
          -- arg <- something like what FUZxxl did..
          when (length args == 1) (putStrLn $ "Using command line arg: " ++ arg)
          -- continue using arg...

您还可以以类似的方式使用 when 的表兄弟 unless

This doesn't fit your specific use case, but the question title made me think immediately of when from Control.Monad. Straight from the docs:

when :: Monad m => Bool -> m () -> m ()

Conditional execution of monadic expressions.

Example:

main = do args <- getArgs
          -- arg <- something like what FUZxxl did..
          when (length args == 1) (putStrLn $ "Using command line arg: " ++ arg)
          -- continue using arg...

You can also use when's cousin unless in similar fashion.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文