3
Haskell新手在這裏。我需要一些幫助,爲全加器編寫一個函數。 我有以下幾點: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)
,我發現了以下錯誤:
* 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)'
但是你的函數不返回兩個元素? –
我認爲你可能正在尋找'\'xor \''(使其成爲中綴所需的反引號)。因爲現在你正在對'xor'和'b'這兩個參數應用'a'。 – ryachza
您目前實現了一個完整的功能,然後嘗試調試它失敗的原因。如果你從小處開始建設,你可以讓自己變得更容易。例如,'fulladder a b c = True ++ True'可能是一個很好的第一個虛擬實現:它失敗了,但它更短,更容易查找。一旦你解決了這個問題,你可以擴展到'ful = bc =(True xor True,True)',等等,直到你完全表達。 –