问题描述
所以我想让我看一个单词列表并返回一个列表,就像它一样,但
,每当它们显示为连续单词时,会进行以下替换。
其中一个例子是 you
,并将它变成 u
我给出以下内容:
hep :: [ Word] - > [Word]
类型字=字符串
现在给我的问题是, m试图使用case表达式,这样我就不必重复代码,但我得到以下错误:
无法匹配预期类型'Char'的实际类型为'[Char]'
在模式中:You
在另一种情况下:You - > u:hep xs
在表达式中:{你 - > u:hep xs}
来自以下代码
hep [] = []
hep [a:xs] =
的情况a您 - > u:hep xs
任何人都可以告诉我问题是什么?
编辑:
我已经添加了以下代码:
hep [] = [[]]
hep(a:xs)=
you的情况a - > u:hep xs
是 - > r:hep xs
your - > ur:hep xs
男朋友 - > bf:hep xs
女朋友 - > gf:hep xs
很棒 - > gr8:hep xs
a - > a:hep xs
现在我将如何添加一个案例,以便如果列表包含2或3个特定的词,我可以把它变成一个首字母缩写词吗?
Ex
[By,The,你可以在这里找到你想要的东西,然后你就可以在这里找到你想要的东西了。[btw]]
重试与字符串列表匹配,但是 hep
的类型是 [Word] - > [Word]
,这与此相矛盾。这个错误信息是指这个事实。
但是我猜你实际上想要的是什么?
<$ (a:xs)= a
的情况aYou - >> u:hep xs
a - > a:hep xs
Hey guys so I'm suppose to takes a list of words and returns a list just like it butwith the following substitutions made each time they appear as consecutive words.
One example is you
and turn it into u
I'm given the following:
hep :: [Word] -> [Word]
type Word = String
now what is giving me problem is that I'm trying to use case expressions so that I won't have to repeat code but I get the following error
Couldn't match expected type `Char' with actual type `[Char]'
In the pattern: "You"
In a case alternative: "You" -> "u" : hep xs
In the expression: case a of { "You" -> "u" : hep xs }
from the following code
hep [] = []
hep [a:xs] = case a of
"You" -> "u":hep xs
Anyone tell me what the problem is?
Edit:
I have added the following code
hep [] = [[]]
hep (a:xs) = case a of
"you" -> "u":hep xs
"are" -> "r":hep xs
"your" -> "ur":hep xs
"boyfriend" -> "bf":hep xs
"girlfriend" -> "gf":hep xs
"great" -> "gr8":hep xs
a -> a:hep xs
Now how would I be able to add a case so that if the list contains 2 or 3 certain words in an order, I could turn that into an acronym?
Ex
["By","The","way"] = ["btw"]
You're trying to match against a list of a list of Strings, but the type of hep
is [Word] -> [Word]
, which contradicts that. The error message is referring to this fact.
But I'm guessing what you actually want is this?
hep [] = []
hep (a:xs) = case a of
"You" -> "u":hep xs
a -> a:hep xs
这篇关于理解Haskell类型的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!