Haskell 中的铸造
a) 我需要在 haskell 中从 String
转换为 int
。我有一个函数可以将句子中的第三个单词作为字符串获取,但是我所有句子中的第三个单词都是数字(int),我如何从字符串转换为int,以便之后我可以使用数字来执行类似的操作添加还是乘法?
getThirdWord :: String -> String
getThirdWord = head . tail . tail . words
b) 我正在使用 Visual Haskell Studio。如何在 Visual Haskell Studio 中使用 map
和 zip
等函数?我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Yacoby的答案当然是正确的。让我说两点:
read
非常笼统。它不仅将String
转换为Int
。它的返回类型取决于上下文。在这种情况下,getThirdWord
被定义为String -> Int
,因此read
知道要做什么。情况并非总是如此,因此如果您在使用read
时遇到编译错误,请记住这一点:您可能需要帮助Haskell找出返回类型。当前您使用
head 。尾巴 。 tail
获取第三个列表元素。如果您想要第 23 个元素怎么办?更易于维护和可读的解决方案是使用(!!)
:这样您就可以在任何索引处检索列表元素。因此:(请注意 2 而不是 3,因为索引是 0 索引的。)
Yacoby's answer is correct of course. Let me make two remarks:
read
is very general. It not only convertsString
toInt
. Its return type depends on the context. In this casegetThirdWord
is defined to beString -> Int
, soread
knows what to do. This is not always the case, so if you ever get a compile error while usingread
, remember this: you may need to help Haskell figure out the return type.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:(Note the 2 instead of a 3, since indices are 0-indexed.)
关于转换,请查看 read 函数。
Visual Haskell Studio 似乎是一个 IDE,因为后端使用 GHC,完全支持Haskell规范,并且包含包含map和fold的库。
Regarding casting, take a look at the read function.
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.
我支持 Yacoby 使用
read
,但请记住head
/tail
和read
都是部分函数,在空列表上可能会失败。我认为如果可以使用模式匹配,最好避免使用head
。例如,此函数对于任何输入都是安全的(如果单词少于 3 个,它只返回一个空字符串)。当然,如果您绝对确定列表始终非空,则可以使用
head
/tail
。使用
read
,您可以捕获异常(不是很方便),或者使用reads
代替:此
toInt
返回Just
如果无法解析,则为数字或Nothing
。或者,您可以使用 安全库 及其readMay
函数。I second Yacoby to use
read
, but keep in mind that bothhead
/tail
andread
are partial functions and may fail on empty list. I think it's better to avoidhead
if you can use pattern matching instead. For example,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 usereads
instead:This
toInt
returnsJust
a number orNothing
if it cannot parse. Alternatively you can use Safe library and itsreadMay
function.你们都好吧,但我发帖是因为我认为在学习帮助阅读和展示时,我想到了一个非常优雅且有用的功能文本。
有用吧? =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.
Useful, eh? =P. Also xs !! n gets xs's n'th element. Look at others answers for easy ways to print.