0
我有一個函數penceToPounds :: [Int] -> [Float]
是轉變便士來斤。我必須解決這個使用map
短地圖在Haskell
一個很長的路要走,以做到這一點是:
penceToPounds xs = map ptp xs
where ptp x = fromIntegral x/100
怎樣包括fromIntegral如果我想寫功能的較短的版本,如:
penceToPounds = map (`div` 100)
if if應該是'penceToPounds = map((/ 100)。 fromIntegral)',因爲'div'是積分值的劃分。另外,應該使用'fmap',因爲它比'map'更普遍。 – zigazou
'<$>'更加通用 –
@zigazou更一般的並不意味着在所有情況下都更好。我會使用'map',因爲無論如何這個函數被限制在列表上,當我看到'map'時,我立即知道我只需要擔心列表。泛化可以是很好的,但如果你總是儘可能使所有東西都儘可能一般,那麼事情會變得很難閱讀。 –