Haskell-字符串仅由另一个字符串

时间:2018-05-04 16:08:24

标签: haskell

我想检查用户给出的字符串是否仅由先前作为程序参数提供的另一个字符串的字符组成

我在haskell搜索过每一个地方,但我找不到任何东西(找到其他语言的答案但不是haskell) 我知道如何在python或C中执行此操作,但这是我的第一个haskell程序

这是我到目前为止所做的:

我正在调用字符串的功能+设置来检查

myCheck first second = do
-- some stuff
let alpha = "01" -- normally this is given with argument by the user but just for simplicity let's do it that way (it could also be alpha = "aziuefè!çè")
suite <- getLine
if isValid suite alpha
  then putStr "OK\n"
  else exit
-- some other stuff

现在是有效函数isValid&#34;&#34; alpha = True非常重要,因为我需要稍后在代码

中处理它
isValid :: String -> String -> Bool                                                                       
isValid "" alpha = True                                                                                   
isValid xs alpha =                                                                                      
  case dropWhile (isAlphabet xs alpha) of                                                                  
    "" -> True                                                                                             
   ('.':ys) -> all (isAlphabet ys alpha)                                                                  
   _ -> False

isAlphabet功能

isAlphabet :: Char -> String -> Bool                                                                     
isAlphabet xs alpha = xs `elem` alpha

但是(我在使用haskell时开始习惯)

 /Users/mgial/tech2/FUN_deBruijn_2017/app/Main.hs:29:19: error:
• Couldn't match expected type ‘a0 -> Bool’ with actual type ‘Bool’
• Possible cause: ‘isAlphabet’ is applied to too many arguments
  In the first argument of ‘dropWhile’, namely
    ‘(isAlphabet xs alpha)’
  In the expression: dropWhile (isAlphabet xs alpha)
  In the expression:
    case dropWhile (isAlphabet xs alpha) of
      "" -> True
      ('.' : ys) -> all (isAlphabet ys alpha)
      _ -> False
     |
  29 |   case dropWhile (isAlphabet xs alpha) of
     |                   ^^^^^^^^^^^^^^^^^^^
/Users/mgial/tech2/FUN_deBruijn_2017/app/Main.hs:29:30: error:
• Couldn't match type ‘[Char]’ with ‘Char’
  Expected type: Char
    Actual type: String
• In the first argument of ‘isAlphabet’, namely ‘xs’
  In the first argument of ‘dropWhile’, namely
    ‘(isAlphabet xs alpha)’
  In the expression: dropWhile (isAlphabet xs alpha)
   |
29 |   case dropWhile (isAlphabet xs alpha) of
   |                              ^^

/Users/mgial/tech2/FUN_deBruijn_2017/app/Main.hs:30:5: error:
    • Couldn't match expected type ‘[a0] -> [a0]’
              with actual type ‘[Char]’
    • In the pattern: ""
     In a case alternative: "" -> True
     In the expression:
    case dropWhile (isAlphabet xs alpha) of
      "" -> True
      ('.' : ys) -> all (isAlphabet ys alpha)
      _ -> False
   |
30 |     "" -> True
   |     ^^

/Users/mgial/tech2/FUN_deBruijn_2017/app/Main.hs:31:6: error:
 • Couldn't match expected type ‘[a0] -> [a0]’
               with actual type ‘[Char]’
 • In the pattern: '.' : ys
  In a case alternative: ('.' : ys) -> all (isAlphabet ys alpha)
  In the expression:
    case dropWhile (isAlphabet xs alpha) of
      "" -> True
      ('.' : ys) -> all (isAlphabet ys alpha)
      _ -> False
   |
31 |     ('.':ys) -> all (isAlphabet ys alpha)
   |      ^^^^^^

/Users/mgial/tech2/FUN_deBruijn_2017/app/Main.hs:31:17: error:
  • Couldn't match expected type ‘Bool’
              with actual type ‘t0 a1 -> Bool’
  • Probable cause: ‘all’ is applied to too few arguments
  In the expression: all (isAlphabet ys alpha)
  In a case alternative: ('.' : ys) -> all (isAlphabet ys alpha)
  In the expression:
    case dropWhile (isAlphabet xs alpha) of
      "" -> True
      ('.' : ys) -> all (isAlphabet ys alpha)
      _ -> False
   |
31 |     ('.':ys) -> all (isAlphabet ys alpha)
   |                 ^^^^^^^^^^^^^^^^^^^^^^^^^

/Users/mgial/tech2/FUN_deBruijn_2017/app/Main.hs:31:22: error:
   • Couldn't match expected type ‘a1 -> Bool’ with actual type ‘Bool’
   • Possible cause: ‘isAlphabet’ is applied to too many arguments
    In the first argument of ‘all’, namely ‘(isAlphabet ys alpha)’
    In the expression: all (isAlphabet ys alpha)
    In a case alternative: ('.' : ys) -> all (isAlphabet ys alpha)
   |
31 |     ('.':ys) -> all (isAlphabet ys alpha)
   |                      ^^^^^^^^^^^^^^^^^^^

/Users/mgial/tech2/FUN_deBruijn_2017/app/Main.hs:31:33: error:
  • Couldn't match expected type ‘Char’ with actual type ‘[Char]’
  • In the first argument of ‘isAlphabet’, namely ‘ys’
    In the first argument of ‘all’, namely ‘(isAlphabet ys alpha)’
    In the expression: all (isAlphabet ys alpha)
   |
31 |     ('.':ys) -> all (isAlphabet ys alpha)
   |                                 ^^

这个特定的功能不是家庭作业,而是一个更大的项目的一部分 谢谢,真的希望我没有错过任何已经回答的问题

通过回答建议编辑:

删除了isAlphabet(这个函数没有任何意义,因为它只调用了elem)

isValid :: String -> String -> Bool                                                                    
isValid "" alpha = True                                                                              
isValid xs alpha =                                                                                         
  case dropWhile (xs `elem` alpha) of                                                                      
    "" -> True                                                                                             
    ('.':ys) -> all (`elem` alpha) ys                                                                      
    _ -> False

编译错误:

/Users/mgial/tech2/FUN_deBruijn_2017/app/Main.hs:26:19: error:
    • Couldn't match expected type ‘a0 -> Bool’ with actual type ‘Bool’
    • Possible cause: ‘elem’ is applied to too many arguments
     In the first argument of ‘dropWhile’, namely ‘(xs `elem` alpha)’
      In the expression: dropWhile (xs `elem` alpha)
      In the expression:
        case dropWhile (xs `elem` alpha) of
          "" -> True
         ('.' : ys) -> all (`elem` alpha) ys
         _ -> False
   |
26 |   case dropWhile (xs `elem` alpha) of
   |                   ^^^^^^^^^^^^^^^

/Users/mgial/tech2/FUN_deBruijn_2017/app/Main.hs:26:29: error:
    • Couldn't match type ‘Char’ with ‘[Char]’
      Expected type: [String]
        Actual type: String
    • In the second argument of ‘elem’, namely ‘alpha’
      In the first argument of ‘dropWhile’, namely ‘(xs `elem` alpha)’
      In the expression: dropWhile (xs `elem` alpha)
   |
26 |   case dropWhile (xs `elem` alpha) of
   |                             ^^^^^

/Users/mgial/tech2/FUN_deBruijn_2017/app/Main.hs:27:5: error:
    • Couldn't match expected type ‘[a0] -> [a0]’
                  with actual type ‘[Char]’
    • In the pattern: ""
      In a case alternative: "" -> True
      In the expression:
        case dropWhile (xs `elem` alpha) of
          "" -> True
          ('.' : ys) -> all (`elem` alpha) ys
          _ -> False
   |
27 |     "" -> True
   |     ^^

/Users/mgial/tech2/FUN_deBruijn_2017/app/Main.hs:28:6: error:
    • Couldn't match expected type ‘[a0] -> [a0]’
                  with actual type ‘[Char]’
    • In the pattern: '.' : ys
      In a case alternative: ('.' : ys) -> all (`elem` alpha) ys
      In the expression:
        case dropWhile (xs `elem` alpha) of
          "" -> True
          ('.' : ys) -> all (`elem` alpha) ys
          _ -> False
   |
28 |     ('.':ys) -> all (`elem` alpha) ys
   |      ^^^^^^

最终编辑:

isValid :: String -> String -> Bool                                                                     
isValid "" alpha = True                                                                                 
isValid xs alpha =                                                                                         
  case dropWhile (`elem` alpha) xs of                                                                      
    "" -> True                                                                                             
    ('.':ys) -> all (`elem` alpha) ys                                                                      
    _ -> False

1 个答案:

答案 0 :(得分:4)

使用all (isAlphabet ys alpha),您首先尝试将isAlphabet应用于整个字符串 ys和字母表,然后以某种方式将结果通过{ {1}}。但是,类型不支持这个:

all
您想要做的是使用 isAlphabet :: Char -> String -> Bool │ │ │ ys :: String │ │ alpha :: String │ all :: (a->Bool) -> [a] -> Bool all分别应用于每个字符 。这就是它的用途。

isAlphabet

请注意,如果你定义了 ... all (\c -> isAlphabet c alpha) ys ... 函数交换参数(这将是更自然的顺序),那么你可以缩短(eta-reduce)这个

isAlphabet

或者,您可以使用运算符部分直接应用第二个参数。事实上,我首先不打算定义isAlphabet :: String -> Char -> Bool ... all (isAlphabet alpha) ys ... ,因为它实际上是isAlphabet的确切同义词。只需写下

elem

  ... all (`elem` alpha) ys ...