作为一个训练练习,我编写了一个多态函数来确定给定数字是单个数字还是所有数字列表的素数:
{-# LANGUAGE FlexibleInstances #-}
class PrimeTo a where
ispt :: Integer -> a -> Bool
instance PrimeTo (Integer) where
ispt n d = 0 /= (rem n d)
instance PrimeTo ([Integer]) where
ispt n [] = True
ispt n (x:xs) = (ispt n x) && (ispt n xs)
为了让它工作,我不得不使用 FlexibleInstances,我对此很满意,但很好奇。
据我了解,在严格的 Haskell 98 下,我需要在实例定义中添加一个类型描述符T:
class PrimeTo a where
ispt :: Integer -> a -> Bool
instance PrimeTo (T Integer) where
ispt n d = 0 /= (rem n d)
instance PrimeTo (T [Integer]) where
ispt n [] = True
ispt n (x:xs) = (ispt n x) && (ispt n xs)
但我不知道用什么代替“T”,我什至不知道这在 Haskell 98 下是否可行。
所以:
- 在 Haskell 98 下这甚至可能吗?
- 如果是这样,T 会使用什么?