haskell - Haskell:无法将类型“Char”与“[Char]”匹配预期类型:[String] 实际类型:String
问题描述
我有这个代码:
censName :: String -> String
censName [] = []
censName xs = unwords(censWords(words xs))
censWords :: [String] -> [String]
censWords [] = []
censWords (x:xs) = if isUpper(head x) then replace(tail x) else censWords xs
replace :: String -> String
replace [] = []
replace (x:xs) = '*':replace xs
错误说:
Couldn't match type ‘Char’ with ‘[Char]’
Expected type: [String]
Actual type: String
我的问题是:为什么!?我的意思是,在 replace(tail x) x 中,x 是一个字符串,那么为什么它需要 [String]?
在此处发布的类似问题上,我找不到任何适合我的问题。
解决方案
censWords :: [String] -> [String]
-- ^^^^^^^^ expected
censWords (x:xs) = if .. then replace(tail x) else ..
-- ^^^^^^^^^^^^^^^ actual value
返回值是单个字符串,但需要一个字符串列表。
问题不在于对 的调用replace
,而在于它返回的值类型错误,而不是预期的值。
推荐阅读
- php - 在 html标签中显示来自 curl 请求的图像
- apache-kafka - Kafka 消费者何时重试?
- ffmpeg - FFMPEG 在剪辑结束时切断音频
- apache-kafka - LogCompaction and consumer in Kafka
- r - Create sequence of values based on multiple column values in R
- node.js - How do I get my async function to return values not a promise?
- makefile - ocamlbuild: Nothing to be done for `all'
- botframework - 是否可以在自适应卡上实现简单的计数器?就像每次单击提交按钮时,文本块中的值都会增加
- solr - SOLR 索引任意数据
- google-cloud-platform - 如何从命令行设置/获取 json 格式的气流变量