haskell中的全加器

T.T*_*T.T 3 haskell boolean-logic

Haskell新手在这里.我需要一些帮助来为全加器编写函数.我有以下内容:

xor :: Bool -> Bool -> Bool
xor True False = True
xor False True = True
xor _ _ = False

fulladder :: Bool -> Bool -> Bool ->(Bool, Bool)
fulladder a b c =  c xor (a xor b) ++ (a&&b) || ((a xor b) && c)      
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

  * Couldn't match expected type `(Bool -> Bool -> Bool)
                                    -> Bool -> Bool'
                  with actual type `Bool'
  * The function `a' is applied to two arguments,
      but its type `Bool' has none
      In the first argument of `(&&)', namely `(a xor b)'
      In the second argument of `(||)', namely `((a xor b) && c)'
Run Code Online (Sandbox Code Playgroud)

Wil*_*sem 8

函数的返回类型是一个元组.确实:

fulladder :: Bool -> Bool -> Bool -> (Bool, Bool)
--                                   ^ 2-tuple
Run Code Online (Sandbox Code Playgroud)

现在(++) :: [a] -> [a] -> [a]连接列表.所以这肯定不会构建一个元组.所以我们解决的第一个错误是:

fulladder :: Bool -> Bool -> Bool ->(Bool, Bool)
fulladder a b c =  ( c xor (a xor b) , (a&&b) || ((a xor b) && c) )
--                 ^ tuple syntax    ^                            ^ 
Run Code Online (Sandbox Code Playgroud)

接下来在Haskell中指定一个函数,后跟参数.所以c xor a不行.你应该使用xor c a(或使用背景).所以我们应该把它重写为:

fulladder :: Bool -> Bool -> Bool ->(Bool, Bool)
fulladder a b c =  ( xor c (xor a b) , (a&&b) || ((xor a b) && c) )
--                   ^      ^ right order          ^
Run Code Online (Sandbox Code Playgroud)

或者:

fulladder :: Bool -> Bool -> Bool ->(Bool, Bool)
fulladder a b c =  ( c `xor` (a `xor` b) , (a&&b) || ((a `xor` b) && c) )
--                     ^   ^    ^   ^ backtics           ^   ^
Run Code Online (Sandbox Code Playgroud)

现在函数生成:

*Main> fulladder False False False
(False,False)
*Main> fulladder False False True
(True,False)
*Main> fulladder False True False
(True,False)
*Main> fulladder False True True
(False,True)
*Main> fulladder True False False
(True,False)
*Main> fulladder True False True
(False,True)
*Main> fulladder True True False
(False,True)
*Main> fulladder True True True
(True,True)
Run Code Online (Sandbox Code Playgroud)

哪个是输出携带元组的正确结果.