【问题标题】:F# Using List.map on an array of stringsF# 在字符串数组上使用 List.map
【发布时间】:2016-01-24 19:53:17
【问题描述】:

我正在尝试使用 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# 没有意识到 input.split 的结果可以与 map 函数一起使用,因为它是一个字符串数组。我只是不知道如何更改代码以完成我想要完成的任务。我对 F# 比较陌生,所以我能得到任何帮助将不胜感激!

【问题讨论】:

    标签: lambda f# functional-programming


    【解决方案1】:

    F# 没有实现 input.split 的结果,可以与 map 函数一起使用,因为它是一个字符串数组

    List.map 作用于lists,因此 F# 实现了结果不能List.map 一起使用。请改用Array.map(适用于数组)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-20
      • 2018-10-10
      • 2018-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多