Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想用 foldr 或 foldMap 实现最小值。根据练习,它应该有这样的定义:
mini :: (Foldable t, Ord a) => t a -> Maybe a -- named "mini" to avoid name clash
这听起来很简单,但我不知道我可以用什么代替下面的 X 来使它工作。请帮忙?
mini xs = Just (foldr min X xs)
你也可以通过向我展示如何使用 foldMap 来获得奖励积分,但这似乎更难。
你可以这样做:
mini :: (Foldable f, Ord a) => f a -> Maybe a mini = foldr maybeMin Nothing where maybeMin x Nothing = Just x maybeMin x (Just y) = Just (min x y)
您可以尝试将“X”替换为列表中的第一个元素。当然,您现在应该处理使用空列表调用函数“mini”的情况。