我有一個字串串列 ["Hi", "hELLO", "nO"]
我想將所有內容都轉換為小寫。我為此寫了一個函式:
import Data.Char(toLower)
makeSmall xs = map toLower xs
它編譯,但是當我這樣做時,makeSmall ["Hi", "hELLO", "nO"]它給了我這個錯誤
<interactive>:2:12: error:
? Couldn't match expected type ‘Char’ with actual type ‘[Char]’
? In the expression: "Hi"
In the first argument of ‘makeSmall’, namely
‘["Hi", "hELLO", "nO"]’
In the expression: makeSmall ["Hi", "hELLO", "nO"]
<interactive>:2:17: error:
? Couldn't match expected type ‘Char’ with actual type ‘[Char]’
? In the expression: "hELLO"
In the first argument of ‘makeSmall’, namely
‘["Hi", "hELLO", "nO"]’
In the expression: makeSmall ["Hi", "hELLO", "nO"]
<interactive>:2:25: error:
? Couldn't match expected type ‘Char’ with actual type ‘[Char]’
? In the expression: "nO"
In the first argument of ‘makeSmall’, namely
‘["Hi", "hELLO", "nO"]’
In the expression: makeSmall ["Hi", "hELLO", "nO"]
我試圖了解如何使我的函式適用于字串串列,而不僅僅是字串
uj5u.com熱心網友回復:
toLower :: Char -> Char將 a 映射Char到 a Char,這意味著map toLower將采用單個 String,它將回傳 a String。
如果你想處理一個串列的StringS,你應該使用第二個map:
makeSmall :: [String] -> [String]
makeSmall xs = map (map toLower) xs
或更短:
makeSmall :: [String] -> [String]
makeSmall = map (map toLower)
因此,我們制作了一個map toLower用作地圖功能的映射器。因此,此功能將在單個 String. 因為我們這樣做了這樣的映射,我們對一個作業串列的String秒。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/352612.html
標籤:哈斯克尔
下一篇:建立一個長度為N的串列
