用haskell中的选定字符分割字符串 [英] Split a string by a chosen character in haskell

查看:120
本文介绍了用haskell中的选定字符分割字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当有选定的字符时,我都试图拆分字符串.因此,如果我收到"1,2,3,4,5",并且我选择的字符是",",则结果将是一个列表,例如["1","2","3","4","5"].

I'm trying to split a string every time there is a chosen character. So if I receive "1,2,3,4,5", and my chosen character is "," the result is a list such as ["1","2","3","4","5"].

我一直在浏览这里已经回答的问题,他们指出我使用splitOn.但是,当我尝试使用import Data.List.Split时,Haskell给了我以下错误:找不到模块‘Data.List.Split’.当我尝试仅使用splitOn而不导入模块时,它显示了变量不在范围内:s​​plitOn .

I've been looking through the already answered questions in here and they point me to using splitOn. However, when i try to import Data.List.Split in order to use it, Haskell gives me the following error: Could not find module ‘Data.List.Split’ . When I tried to just use splitOnwithout importing the module, it showed me Variable not in scope: splitOn.

我的问题是

  1. 我遇到此错误是正常现象吗? splitOn是可行的选择还是我应该尝试其他方法?
  2. 还有哪些其他简单的解决方案?
  1. Is it normal that i'm getting this error? Is splitOn a viable option or should I just try something else?
  2. What other simple solutions are there?

我可以写一些可以为我做的事情,但是我想知道为什么我不能导入Data.List.Split,以及那里是否还有我看不到的其他更简单的选项.谢谢!

I can just write something that will do this for me but I'm wondering why I'm not able to import Data.List.Split and if there are other simpler options out there that I'm not seeing. Thank you!

推荐答案

如果您使用的是 GHC 随附了标准的Prelude和基本软件包中的模块其他一些软件包.

If you're using GHC it comes with the standard Prelude and the modules in the base package, and perhaps a few other packages.

大多数包,例如拆分包(包含Data.List.Split模块),不是GHC本身的一部分.您必须将它们作为显式编译步骤导入.使用构建工具最容易做到这一点.大多数Haskeller使用 Cabal

Most packages, like the split package (which contains the Data.List.Split module), aren't part of GHC itself. You'll have to import them as an explicit compilation step. This is easiest done with a build tool. Most Haskellers use either Cabal or Stack.

例如,使用Stack,您可以将split软件包添加到您的package.yaml文件中:

With Stack, for example, you can add the split package to your package.yaml file:

dependencies:
- base >= 4.7 && < 5
- split

您还可以使用Stack启动GHCi时加载一个额外的程序包.这对临时实验很有用.

You can also load an extra package when you use Stack to start GHCi. This is useful for ad-hoc experiments.

这篇关于用haskell中的选定字符分割字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆