[英]Haskell Function with “split” cases
How can I make a "split"ting case inside an function like this: 我如何在这样的函数中创建一个“拆分”案例:
getBase64 :: String -> [Char]
getBase64 code
| ("[&":c:r) = c
| _ = ' '
This is so not working cause: 这是行不通的原因:
main.hs:7:11: Not in scope: ‘c’
main.hs:7:13: Not in scope: ‘r’
main.hs:7:18: Not in scope: ‘c’
The error is clear to me, but I'm a newcommer and don't know how I can do my "wish" ... 该错误对我来说很明显,但是我是一个新来的人,不知道该如何做我的“愿望” ...
Thanks for help! 感谢帮助!
You are trying to match a pattern, but since you are using the | cond = expr
您正在尝试匹配模式,但是由于使用的是
| cond = expr
| cond = expr
notation, you are actually using a guard. | cond = expr
表示法,实际上是在使用防护。 Visit this page for more details about guards: https://en.wikibooks.org/wiki/Haskell/Control_structures#if_and_guards_revisited . 请访问此页面以获取有关警卫的更多详细信息: https : //en.wikibooks.org/wiki/Haskell/Control_structures#if_and_guards_revisited 。
| ("[&":c:r)
| ("[&":c:r)
actually means "a list constructed from the string "[&"
and value c
and value r
", and Haskell compiler complains the variables c
and r
do not exist. | ("[&":c:r)
实际含义是“一个由字符串"[&"
以及值c
和值r
构成的列表,Haskell编译器抱怨变量c
和r
不存在。
Here is how to define functions with multiple cases: 以下是定义具有多种情况的函数的方法:
getBase64 ("[&":c:r) = c
getBase64 _ = ' '
However, this won't work because "[&"
is a string, and Haskell thinks the first parameter is a list of strings. 但是,这将不起作用,因为
"[&"
是一个字符串,Haskell认为第一个参数是一个字符串列表。 A string is a list of characters and you need to fix your pattern like this: 字符串是字符列表,您需要像这样修复模式:
getBase64 ('[':'&':c:r) = c
getBase64 _ = ' '
Look at these examples for more details about pattern matching against characters in a string: https://stackoverflow.com/a/1602296/303939 and https://stackoverflow.com/a/3851126/303939 请查看以下示例,以获取有关针对字符串中的字符进行模式匹配的更多详细信息: https : //stackoverflow.com/a/1602296/303939和https://stackoverflow.com/a/3851126/303939
Then there is another problem: getBase64
returns [Char]
. 然后还有另一个问题:
getBase64
返回[Char]
。 If that's what you intended, your code should be fixed like this: 如果这正是您的预期,则应按以下方式修复您的代码:
getBase64 ('[':'&':c:r) = [c]
getBase64 _ = " " -- or [' ']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.