f#函数,当给定一个字符串和一个整数时,它从字符串的位置返回一个字符(整数)



我有一个函数返回char在一个位置:

let NthChar inpStr indxNum = 
    if inpStr.Length >=1 then printfn "Character %s" inpStr.[indxNum];
    else printfn "Not enough arguments"

错误

inpStr.Length 

inpStr.[indxNum]
错误跟踪

:

基于此程序点之前的信息查找不确定类型的对象。在此程序点之前可能需要类型注释来约束对象的类型。这可能允许解析查找。

您正在尝试"dot into"某事并访问它的索引器。除了当你这样做的时候,某些东西的类型仍然是未知的(可能它没有索引器)
这就得到了明确的错误信息这也给了你删除它的方法,只需添加一个类型注释:

// code shortened
let nthChar (inpStr : string) indxNum = inpStr.[indxNum]
// alternative syntax
let nthChar inpStr indxNum = (inpStr : string).[indxNum]

相关内容