我寫這個函數:爲什麼這些表達式具有不同程度的模糊性?
||| Returns the ten largest values in the list.
top_ten : Ord a => List a -> List a
我第一次嘗試是一個pointfree實現使用功能組成:
top_ten = take 10 . reverse . sort
但是這給了以下錯誤:
Main.idr:3:9:When checking right hand side of top_ten with expected type
List a -> List a
Can't disambiguate name: Prelude.List.take, Prelude.Stream.take
我的第二次嘗試是一個直截了當的有針對性的實施:
top_ten xs = take 10 (reverse (sort xs))
這工作,因爲做這些:
top_ten xs = take 10 $ reverse $ sort xs
top_ten xs = take 10 (reverse $ sort xs)
top_ten xs = take 10 $ reverse . sort $ xs
top_ten xs = take 10 (reverse . sort $ xs)
然而,這些並不:
top_ten xs = take 10 . reverse $ sort xs
top_ten xs = take 10 . reverse . sort $ xs
top_ten xs = take 10 $ (reverse . sort) xs
top_ten xs = (take 10 . reverse) (sort xs)
top_ten xs = take 10 ((reverse . sort) xs)
究竟是怎麼回事呢? 什麼導致這些等價表達式具有不同的模糊程度?
伊德里斯類型推斷還不如能夠在哈斯克爾/阿格達。 –