地图功能不正常?

时间:2016-11-20 07:14:11

标签: haskell

地图功能无法在此行上运行

compPic (a:as) = map (compL a) as

它只适用于单个字符串,但不能在字符串列表中找到错误。

compL  :: String -> String
compL  bs = [a | b <- bs, a <- 
                if (b == ' ')
                   then "X"
                 else if (b=='X')
                   then " "
                   else [b]]


compPic :: [String] -> [String]
compPic  [] = []
compPic  (a:as) = map (compL a) as

1 个答案:

答案 0 :(得分:2)

让我们看一下mapPrelude的定义:

map _ []     = []
map f (x:xs) = f x : map f xs

在这里,我们看到map被定义为将列表中的每个值传递给正在应用的函数。此外,我们不需要基本情况​​,因为map处理递归。因此,您应该将compPic重写为:

compPic :: [String] -> [String]
compPic as = map compL as

或者,eta-reduced:

compPic = map compL