Haskell 中的铸造

发布于 2024-08-12 08:11:39 字数 416 浏览 1 评论 0原文

a) 我需要在 haskell 中从 String 转换为 int 。我有一个函数可以将句子中的第三个单词作为字符串获取,但是我所有句子中的第三个单词都是数字(int),我如何从字符串转换为int,以便之后我可以使用数字来执行类似的操作添加还是乘法?

getThirdWord :: String -> String
getThirdWord = head . tail . tail . words

b) 我正在使用 Visual Haskell Studio。如何在 Visual Haskell Studio 中使用 mapzip 等函数?我的 VHS 中是否需要添加任何插件才能使其正常工作?

提前非常感谢!

a) I need to cast from String to int in haskell. I have a function that gets the third word in a sentence as a string, but my third word in all my sentences are numbers (int), how can I cast from string to int so then I can use the number afterwards to do operations like add or mult?

getThirdWord :: String -> String
getThirdWord = head . tail . tail . words

b) I'm using Visual Haskell Studio. How can I use functions like map and zip in visual haskell studio? Are there any plugins that I need to include to my vhs to make them work?

Thank you so much in advance!

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

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

发布评论

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

评论(4

阳光下的泡沫是彩色的 2024-08-19 08:11:39

Yacoby的答案当然是正确的。让我说两点:

  1. read 非常笼统。它不仅将 String 转换为 Int。它的返回类型取决于上下文。在这种情况下,getThirdWord 被定义为String -> Int,因此 read 知道要做什么。情况并非总是如此,因此如果您在使用read时遇到编译错误,请记住这一点:您可能需要帮助Haskell找出返回类型。
  2. 当前您使用 head 。尾巴 。 tail 获取第三个列表元素。如果您想要第 23 个元素怎么办?更易于维护和可读的解决方案是使用 (!!):这样您就可以在任何索引处检索列表元素。因此:

    thirdWordAsInt::String -> INT 
    ThirdWordAsInt = 读取。 (!!2) 。字
    

    (请注意 2 而不是 3,因为索引是 0 索引的。)

Yacoby's answer is correct of course. Let me make two remarks:

  1. read is very general. It not only converts String to Int. Its return type depends on the context. In this case getThirdWord is defined to be String -> Int, so read knows what to do. This is not always the case, so if you ever get a compile error while using read, remember this: you may need to help Haskell figure out the return type.
  2. Currently you use head . tail . tail to get the third list element. What if you wanted, say, the 23rd element? A more maintainable and readable solution is to use (!!): this way you can retrieve a list element at any index. Thus:

    thirdWordAsInt :: String -> Int 
    thirdWordAsInt = read . (!! 2) . words
    

    (Note the 2 instead of a 3, since indices are 0-indexed.)

挥剑断情 2024-08-19 08:11:39

关于转换,请查看 read 函数。

getThirdWord :: String -> Int
getThirdWord = read . head . tail . tail . words

Visual Haskell Studio 似乎是一个 IDE,因为后端使用 GHC,完全支持Haskell规范,并且包含包含map和fold的库。

Regarding casting, take a look at the read function.

getThirdWord :: String -> Int
getThirdWord = read . head . tail . tail . words

Visual Haskell Studio seems to be an IDE only, in that the backend uses GHC, which fully supports the Haskell specification and includes the libraries that include map and fold.

巷子口的你 2024-08-19 08:11:39

我支持 Yacoby 使用 read,但请记住 head/tailread 都是部分函数,在空列表上可能会失败。我认为如果可以使用模式匹配,最好避免使用 head 。例如,

get3rd :: String -> String
get3rd s =
  case (take 3 $ words s) of
    [_,_,w]   -> w
    otherwise -> ""

此函数对于任何输入都是安全的(如果单词少于 3 个,它只返回一个空字符串)。当然,如果您绝对确定列表始终非空,则可以使用head/tail

使用read,您可以捕获异常(不是很方便),或者使用reads代替:

toInt :: String -> Maybe Int
toInt s =
  case reads s of
    [(i,_)]   -> Just i
    otherwise -> Nothing

-- test cases
main = do
  print . toInt . get3rd $ "1 2 3"
  print . toInt . get3rd $ "one two three"
  print . toInt . get3rd $ "short list"

toInt返回Just如果无法解析,则为数字或 Nothing。或者,您可以使用 安全库 及其 readMay 函数。

I second Yacoby to use read, but keep in mind that both head/tail and read are partial functions and may fail on empty list. I think it's better to avoid head if you can use pattern matching instead. For example,

get3rd :: String -> String
get3rd s =
  case (take 3 $ words s) of
    [_,_,w]   -> w
    otherwise -> ""

This function is safe for any input (it just returns an empty string if there are less than 3 words). Certainly, if you are absolutely sure the lists are always non-empty, you can use head/tail.

With read you can either catch exceptions (not very convenient), or use reads instead:

toInt :: String -> Maybe Int
toInt s =
  case reads s of
    [(i,_)]   -> Just i
    otherwise -> Nothing

-- test cases
main = do
  print . toInt . get3rd $ "1 2 3"
  print . toInt . get3rd $ "one two three"
  print . toInt . get3rd $ "short list"

This toInt returns Just a number or Nothing if it cannot parse. Alternatively you can use Safe library and its readMay function.

℉服软 2024-08-19 08:11:39

你们都好吧,但我发帖是因为我认为在学习帮助阅读展示时,我想到了一个非常优雅且有用的功能文本。

as :: (Read a, Show a) => (a -> a) -> String -> String
as f = show . f . read

Prelude> as (+1) "7"
"8"

Prelude> as (+(1/2)) "5"
"5.5"

有用吧? =P。还有xs!! n 获取 xs 的第 n 个元素。查看其他答案以了解简单的打印方法。

You guys are all right, but I post because I think there is a very elegant and useful function I came up with when learning to help with the reading and showing of text.

as :: (Read a, Show a) => (a -> a) -> String -> String
as f = show . f . read

Prelude> as (+1) "7"
"8"

Prelude> as (+(1/2)) "5"
"5.5"

Useful, eh? =P. Also xs !! n gets xs's n'th element. Look at others answers for easy ways to print.

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