0
getPara :: (Num [Char])  =>  [Char] -> Integer -> [Char]
getPara "" _ = ""


getPara str nr 
    | (nr == 0 ) && ((head str) == ')' ) = ')' : getPara "" 0 
    | ( nr == 0 ) && ( (head str) == '(' ) = '(' : (getPara (tail str) 0)
    | (nr /= 0 ) && ( (head str) == '(') = (getPara (tail str) nr-1) 
    | (nr == 0 ) && ( (head str) /= '(' ) = (head str) : (getPara (tail str) 0 )
    | otherwise = (getPara (tail str) nr)

我想要做的是从字符串中获取 nr 组括号,我得到的错误是:

Illegal Haskell 98 class constraint in type declaration
*** Expression : getPara
*** Type       : Num [Char] => [Char] -> Integer -> [Char]

问题是什么?

4

2 回答 2

2

从马特的回答开始,让我们美化代码(虽然我没有检查它是否有效)。head首先,列表的模式匹配比很多和要好得多tail

getPara :: [Char] -> Integer -> [Char]
getPara "" _ = ""
getPara (x:xs) nr 
    | (nr == 0 ) && ( x == ')' )  = ')' : getPara "" 0 
    | (nr == 0 ) && ( x == '(' ) = '(' : getPara xs 0
    | (nr /= 0 ) && ( x == '(' )  =  getPara xs (nr-1)  -- here!
    | (nr == 0 ) && ( x /= '(' ) = x : getPara xs 0 
    | otherwise = getPara xs nr

现在您可以进行更多模式匹配:

getPara :: [Char] -> Integer -> [Char]
getPara "" _ = ""
getPara (')':xs) 0                 = ')' : getPara "" 0 
getPara ('(':xs) 0                 = '(' : getPara xs 0
getPara ('(':xs) nr   | nr /= 0    = getPara xs (nr-1)  -- here!
getPara (x:xs) 0      | x /= '('   = x : getPara xs 0 
getPara (_:xs) nr                  = getPara xs nr

[编辑]

正如丹尼尔所指出的,仔细分析会发现剩下的守卫总是真实的。

getPara :: [Char] -> Integer -> [Char]
getPara "" _ = ""
getPara (')':xs) 0   = ')' : getPara "" 0 
getPara ('(':xs) 0   = '(' : getPara xs 0
getPara ('(':xs) nr  = getPara xs (nr-1)  -- here!
getPara (x:xs) 0     = x : getPara xs 0 
getPara (_:xs) nr    = getPara xs nr
于 2012-04-16T17:55:51.040 回答
2

getPara不允许您的类型签名,但根本问题是您在定义的深处缺少括号。如果您将代码更改为:

getPara :: [Char] -> Integer -> [Char]
getPara "" _ = ""

getPara str nr 
    | (nr == 0 ) && ((head str) == ')' )  = ')' : getPara "" 0 
    | (nr == 0 ) && ( (head str) == '(' ) = '(' : (getPara (tail str) 0)
    | (nr /= 0 ) && ( (head str) == '(')  = (getPara (tail str) (nr-1))  -- here!
    | (nr == 0 ) && ( (head str) /= '(' ) = (head str) : (getPara (tail str) 0 )
    | otherwise = (getPara (tail str) nr)

它编译......但我不确定它是否有效。

有两个变化:

  1. 的类型签名getPara
  2. nr-1用括号括起来

    | (nr /= 0 ) && ( (head str) == '(')  = (getPara (tail str) (nr-1))
    
于 2012-04-16T16:11:48.433 回答