[英]problems with understanding types in haskell
Hey guys so I'm suppose to takes a list of words and returns a list just like it but with the following substitutions made each time they appear as consecutive words. 大家好,我想取一个单词列表并像它一样返回一个列表,但是每次当它们作为连续单词出现时都会进行以下替换。
One example is you
and turn it into u
一个例子就是
you
,把它变成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? 现在如何添加一个大小写,以便如果列表中顺序包含2或3个特定单词,我可以将其转换为首字母缩写词吗?
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. 您正在尝试与字符串列表中的列表进行匹配,但是
hep
的类型是[Word] -> [Word]
,这与此矛盾。 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
Something like this? 像这样吗
"By" -> let (y:ys) = xs
in if y=="The" && head ys=="Way"
then "btw": hep (drop 2 xs)
else a:hep xs
Although I wouldn't want to write that 50 times in a row. 尽管我不想连续写50次。 How about this?
这个怎么样?
import Data.List
import Data.Maybe
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
"by" -> matchPhrase 3
"see" -> matchPhrase 2
a -> a:hep xs
where matchPhrase num =
let found = lookup (concat $ take num (a:xs)) phrase_list
in case found of
Just _ -> fromJust found : hep (drop num (a:xs))
Nothing -> a:hep xs
phrase_list = [("bytheway", "btw"), ("seeyou","cu")]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.