1

我想知道,为什么这不起作用(2个错误):

removeCard cardList card e =
  if length $ (filter` cardList) == (length cardList)
  -- the same error 
  -- if length $ filter` cardList == length cardList
  -- if (length $ filter` cardList) == (length cardList)
  then error "The error occured"
  else filter` cardList
  where
    filter` [] = []
    filter` (x:xs) = if x == card then xs else x : filter` xs

如果我只是用一个简单的表达式替换 if if 1 == 2 ...,我会得到另一个错误:

 parse error on input `where'
4

2 回答 2

3

不要在名称中为“过滤器素数”使用反引号。请改用单引号:

  removeCard cardList card e =
    if length (filter' cardList) == (length cardList)
    -- the same error
    -- if length $ filter' cardList == length cardList
    -- if (length $ filter' cardList) == (length cardList)
    then error "The error occured"
    else filter' cardList
    where
      filter' [] = []
      filter' (x:xs) = if x == card then xs else x : filter' xs

反引号用于指示非符号函数名称的中缀表示法。

于 2013-11-02T15:39:24.227 回答
2

反引号 (`) 不是标识符中的合法字符。它们用于将普通的二进制函数编写为中缀运算符,如

(== card) `filter` cardList

您可能想使用素数 (')。

removeCard cardList card e =
  if length $ (filter' cardList) == (length cardList)
  then error "The error occured"
  else filter' cardList
  where
    filter' [] = []
    filter' (x:xs) = if x == card then xs else x : filter' xs
于 2013-11-02T15:36:16.087 回答