我是Haskell的新手,看到了一些关于类型签名的东西,但有几个问题我不太明白。
这是我正在查看的代码:
--mult applies product
mult :: Num a => [a] -> a
mult = foldr (*) 1
--posList filters positive numbers out
posList :: (Ord a, Num a) => [a] -> [a]
posList = filter (>0)
--trueList determines whether all of the members of a list are T or not
trueList :: [Bool] -> Bool
trueList = foldr(&&) True
--evenList determines where all of the members of a list are even or not
evenList :: (Integral a, Foldable t) => t a -> Bool
evenList x = if (((foldr (+) 2 x ) `mod` 2) == 0) then True else False
我知道你可以用不同的方法更容易地完成这些功能,但是我必须使用高阶函数map
、filter
和foldr
来完成我的任务。无论如何,由于是我自己编写了这些函数,所以我理解它们的工作原理,但是我不理解那些术语Integral
、Foldable
的含义是什么?在Haskell中它们又被称为什么?
evenList
了吗?我建议你尝试一下evenList [1,3]
。 - chiFoldable t
的意思是 "t 是一个类似于列表的类型"。 - n. m.(+)
的类型是Num a => a -> a -> a
,因此如果我定义了一个类型Foo
,我想要能够进行加法运算,那么我需要定义(+) :: Foo -> Foo -> Foo
,这只能通过使用Foo
实例化Num
类来完成。 - Dan Robertson