繁体   English   中英

Haskell类型类“输入实例上的解析错误”

[英]Haskell type class “parse error on input instance”

我正在做作业,需要一些帮助任务:在第一步中,我需要为重载函数“ genDrop”定义一个新的类型类。 它的行为应类似于“ drop”(对于Int),但不仅限于Int。 在第二步中,应为Int,Nat,PosNat和Int'类型实例化它。

所以这是我的代码:

class GenDrop a where
     genDrop :: a -> [a] -> [a]


    instance GenDrop Int where
        genDrop 0 s = s
        genDrop n (_:s) | n>0 = genDrop (n-1) s
        genDrop _ [] = []

    instance GenDrop Nat where
        genDrop Zero s = s
        genDrop (Succ n) (_:s)  = genDrop n s
        genDrop _ [] = []

    instance GenDrop PosNat where
        genDrop One (_:s)= s
        genDrop (Succ' n) (_:s) = genDrop n s
        genDrop _ [] = []

    instance GenDrop Int' where
        genDrop Zero' s = s
        genDrop (Plus n) (_:s) = genDrop n s
        genDrop (Minus n) s = s
        genDrop _ [] = []

但是在编译时出现错误:

在输入“实例”上解析错误
实例 GenDrop Int其中

我不知道怎么了。

instance声明不是class声明的一部分:不要像它们缩进一样。 缩进在Haskell中很重要。

这是更正的代码:

class GenDrop a where
    genDrop :: a -> [a] -> [a]

instance GenDrop Int where
    genDrop 0 s = s
    genDrop n (_:s) | n>0 = genDrop (n-1) s
    genDrop _ [] = []

instance GenDrop Nat where
    genDrop Zero s = s
    genDrop (Succ n) (_:s)  = genDrop n s
    genDrop _ [] = []

instance GenDrop PosNat where
    genDrop One (_:s)= s
    genDrop (Succ' n) (_:s) = genDrop n s
    genDrop _ [] = []

instance GenDrop Int' where
    genDrop Zero' s = s
    genDrop (Plus n) (_:s) = genDrop n s
    genDrop (Minus n) s = s
    genDrop _ [] = []

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM