2015-10-25 23 views
1

我想使用F#的List.map函數來調用我寫入數組中每個字符串的函數。下面是我寫F#在字符串數組上使用List.map

(*Takes a string and filters it down to common text characters*) 
let filterWord wordToFilter = 
    Regex.Replace(wordToFilter, "[^a-zA-Z0-9/!\'?.-]", ""); 

這裏的功能是我的主要方法,我把它叫做

(*Main method of the program*) 
[<EntryPoint>] 
let main argsv = 
    let input = File.ReadAllText("Alice in Wonderland.txt"); //Reads all the text into a single string 
    let unfilteredWords = input.Split(' '); 
    let filteredWords = unfilteredWords |> List.map(fun x -> filterWord(x)); 
    0; 

問題是我在我的List.map電話說

得到一個語法錯誤
Error  Type mismatch. Expecting a 
    string [] -> 'a  
but given a 
    'b list -> 'c list  
The type 'string []' does not match the type ''a list' 

將input.split更改爲硬編碼的字符串數組修復了錯誤,因此它與F#沒有實現輸入的結果有關.split可以與map函數一起使用,因爲它是一個字符串數組。我只是不知道如何改變代碼,因此它完成了我想完成的任務。我對F#比較新,所以我可以得到任何幫助,我將不勝感激!

回答

4

F#沒有意識到input.split的結果可能與地圖功能的使用,因爲它是一個字符串數組

List.map作品上list S,所以F#實現的結果不能是與List.map一起使用。改爲使用Array.map(它適用於數組)。

+3

那,或者'Seq.map'。 –

+0

哇,很簡單,謝謝! –

相關問題