instance Num Polinom where
    let
        zmnoziKoef :: [Rational] -> [Rational] -> [Rational]
        zmnoziKoef _ [] = []
        zmnoziKoef [] _ = []
        zmnoziKoef (x:xs) (y:ys) = (x * y) : pomnoziKoef x ys `sestejKoef` (xs `zmnoziKoef` (y:ys))
        sestejKoef :: [Rational] -> [Rational] -> [Rational]
        sestejKoef xs [] = xs
        sestejKoef [] ys = ys
        sestejKoef (x:xs) (y:ys) = (x + y):(sestejKoef xs ys)
        pomnoziKoef :: Rational -> [Rational] -> [Rational]
        pomnoziKoef a = map (a *) 
    negate (Polinom koef) = Polinom $ pomnoziKoef (-1) koef
    (Polinom koef1) + (Polinom koef2) = Polinom $ sestejKoef koef1 koef2
    (Polinom koef1) * (Polinom koef2) = Polinom $ zmnoziKoef koef1 koef2
    fromInteger x = Polinom $ [fromInteger x]因此,这段代码可以工作,但是是否有一种方法可以生成一组辅助函数,这样我就不必在每次定义之后编写它们。
如下所示:
instance Num Polinom where
    negate (Polinom koef) = Polinom $ pomnoziKoef (-1) koef
    (Polinom koef1) + (Polinom koef2) = Polinom $ sestejKoef koef1 koef2
    (Polinom koef1) * (Polinom koef2) = Polinom $ zmnoziKoef koef1 koef2
    fromInteger x = Polinom $ [fromInteger x]
        where
            zmnoziKoef :: [Rational] -> [Rational] -> [Rational]
            zmnoziKoef _ [] = []
            zmnoziKoef [] _ = []
            zmnoziKoef (x:xs) (y:ys) = (x * y) : pomnoziKoef x ys `sestejKoef` (xs `zmnoziKoef` (y:ys))
            sestejKoef :: [Rational] -> [Rational] -> [Rational]
            sestejKoef xs [] = xs
            sestejKoef [] ys = ys
            sestejKoef (x:xs) (y:ys) = (x + y):(sestejKoef xs ys)
            pomnoziKoef :: Rational -> [Rational] -> [Rational]
            pomnoziKoef a = map (a *) 发布于 2016-01-30 19:51:00
不是的。let和where只能通过设计在一个表达式或声明中使用。如果要将它们用作多个声明的辅助函数,只需将它们放在顶层:
zmnoziKoef :: [Rational] -> [Rational] -> [Rational]
zmnoziKoef _ [] = []
zmnoziKoef [] _ = []
zmnoziKoef (x:xs) (y:ys) = (x * y) : pomnoziKoef x ys `sestejKoef` (xs `zmnoziKoef` (y:ys))
sestejKoef :: [Rational] -> [Rational] -> [Rational]
sestejKoef xs [] = xs
sestejKoef [] ys = ys
sestejKoef (x:xs) (y:ys) = (x + y):(sestejKoef xs ys)
pomnoziKoef :: Rational -> [Rational] -> [Rational]
pomnoziKoef a = map (a *) 
instance Num Polinom where
    negate (Polinom koef) = Polinom $ pomnoziKoef (-1) koef
    (Polinom koef1) + (Polinom koef2) = Polinom $ sestejKoef koef1 koef2
    (Polinom koef1) * (Polinom koef2) = Polinom $ zmnoziKoef koef1 koef2
    fromInteger x = Polinom $ [fromInteger x]如果希望将其保持在模块的私有位置,只需使用导出列表避免导出它们:
module MyModule(funcA, funcB, funcC) where
...在上面的代码中,只有funcA、funcB和funcC会在导入模块时导出(加上所有实例)。
https://stackoverflow.com/questions/35103559
复制相似问题