如何在非双引号的字符串上使用 read ?

发布于 2024-11-06 22:04:35 字数 630 浏览 0 评论 0原文

我正在使用 readLn 从控制台读取值。

我想编写一个函数:

requestValue :: String -> IO a  
requestValue s = do  
  putStrLn $ "Please enter a new value for " ++ s   
  readLn

然后我就可以这样做,例如,

changeAge :: Person -> IO Person
changeAge p = do
    age' <- requestValue "age"
    return $ p { age = age'}

changeName :: Person -> IO Person
changeName p = do
    name' <- requestValue "name"
    return $ p { name = name'}

我遇到的问题是 String 的读取实例似乎要求字符串用引号引起来。当我真的只想输入 Fred 时,我不想在控制台中输入 "Fred" 来更改名称。

有没有一种简单的方法可以保持 requestValue 的多态性?

I'm reading values from in from a console using readLn.

I'd like to write a function:

requestValue :: String -> IO a  
requestValue s = do  
  putStrLn $ "Please enter a new value for " ++ s   
  readLn

I'd then be able to do, for example,

changeAge :: Person -> IO Person
changeAge p = do
    age' <- requestValue "age"
    return $ p { age = age'}

changeName :: Person -> IO Person
changeName p = do
    name' <- requestValue "name"
    return $ p { name = name'}

The problem I have is that the read instance of String seems to require the string to be in quotes. I don't want to have to enter "Fred" in the console to change name when I really only want to type in Fred.

Is there an easy way to do this that keeps requestValue polymorphic?

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

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

发布评论

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

评论(2

年少掌心 2024-11-13 22:04:35

由于您想要为用户名添加自己的自定义read 行为,因此执行此操作的方法是实际为读数名称编写一个新实例。为此,我们可以为名称创建一个新类型:

import Control.Arrow (first)

newtype Name = Name { unName :: String }
    deriving (Eq, Ord, Show)

并为其编写一个自定义的读取:

instance Read Name where
    readsPrec n = map (first Name) . readsPrec n . quote
        where quote s = '"' : s ++ ['"'] 

这与字符串的读取实例相同,但我们首先引用字符串,读入后

现在您可以修改 Person 类型以使用 Name 而不是 String

data Person = Person { age :: Int
                     , name :: Name } deriving Show  

并且我们正在做生意:

*Main> changeName (Person 31 (Name "dons"))
Please enter a new value for name
Don
Person {age = 31, name = Name {unName = "Don"}}

Since you want to add your own custom read behavior for user names, the way to do that is to actually write a new instance for readings names. To do that we can create a new type for names:

import Control.Arrow (first)

newtype Name = Name { unName :: String }
    deriving (Eq, Ord, Show)

and write a custom read for it:

instance Read Name where
    readsPrec n = map (first Name) . readsPrec n . quote
        where quote s = '"' : s ++ ['"'] 

this is the same as the read instance for strings, but we first quote the string, after reading it in.

Now you can modify your Person type to use Name instead of String:

data Person = Person { age :: Int
                     , name :: Name } deriving Show  

and we're in business:

*Main> changeName (Person 31 (Name "dons"))
Please enter a new value for name
Don
Person {age = 31, name = Name {unName = "Don"}}
荒路情人 2024-11-13 22:04:35

您需要 getLine,而不是 readLn

You want getLine, not readLn.

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