大家好我正在学习haskell并且我使用这段代码来实现分数但是当我编译它时我收到错误parse error on input ‘|’
我的代码如下:
module Fraccion(Frac,Div,mcd,fraccion) where
data Frac = Div Integer Integer deriving (Eq, Show)
mcd :: Integer -> Integer -> Integer
mcd a b | a > b = mcd (a-b) b
| a < b = mcd a (b-a)
| a == b = a
fraccion :: Integer -> Integer -> Frac
fraccion a b | b == 0 = error "divisor can't be 0"
| b < 0 = fraccion (-a) (-b)
| b > 0 = Div (a `div` mcdab) (b `div` mcdab)
where mcdab = mcd (abs a) b
instance (Num Frac) where
negate (Div a b) = (Div (-a) b)
abs (Div a b) = (Div (abs a) b)
(Div a b) * (Div c d) = fraccion (a*c) (b*d)
(Div a b) + (Div c d) = fraccion (a*d+b*c) (b*d)
(Div a b) - (Div c d) = fraccion (a*d-b*c) (b*d)
fromInteger n = (Div n 1)
signum (Div a b) | a > 0 = 1
| a < 0 + -1
| a == b = 0
instance show Frac where
show (Div a b) = (show a) ++ "/" ++ (show b)
你可以帮我解决这个错误吗?
答案 0 :(得分:2)
第27行:
| a < 0 + -1
一个简单的错字。您将+
放在了=
| a < 0 = -1
您还希望在倒数第二行上大写show
。您的类型的deriving
子句也不应包含Show
,因为您明确定义了一个实例而不是派生它。最后,您的模块导出列表应该如下所示。
module Fraccion(Frac(Div),mcd,fraccion) where