Visual Studio中的F#代码类型不正确
我正在编写一个函数来处理F#中的后缀求值。这就是我目前拥有的评估功能Visual Studio中的F#代码类型不正确,f#,f#-interactive,F#,F# Interactive,我正在编写一个函数来处理F#中的后缀求值。这就是我目前拥有的评估功能 //Postfix evaluation of string expr given variables bound to values as specified in vars let (numSt:int list) = [] let rec innerEval (vars:(List<(string * int)>)) (stack: int list) (expr:string) =
//Postfix evaluation of string expr given variables bound to values as specified in vars
let (numSt:int list) = []
let rec innerEval (vars:(List<(string * int)>)) (stack: int list) (expr:string) =
//Head and Tail for stack, head and tail for expr
let chExpr = Seq.toList expr
match expr with
|[] -> stack.Head
|head::tail ->
if head = '+' then
let pushVal = stack.Head + stack.Tail.Head
let newStack = pushVal::stack.Tail
innerEval vars newStack tail
elif head = '-' then
let pushVal = stack.Head - stack.Tail.Head
let newStack = pushVal::stack.Tail
innerEval vars newStack tail
elif head = '/' then
let pushVal = stack.Head / stack.Tail.Head
let newStack = pushVal::stack.Tail
innerEval vars newStack tail
elif head = '*' then
let pushVal = stack.Head * stack.Tail.Head
let newStack = pushVal::stack.Tail
innerEval vars newStack tail
elif head = '$' then
let fstSt = stack.Head
let sndSt = stack.Tail.Head
let nhead = [fstSt; sndSt]
let st = nhead @ stack.Tail.Tail
innerEval vars st tail
elif head = '@' then
let nhead = tail.Head
let newVars = newVarList nhead vars stack.Head //helper function newVarList
innerEval newVars stack.Tail tail
//else it's a letter that needs to give back a number
else
let addNum = (getVal head vars) //get the number
let newStack = addNum::stack //push that onto stack
innerEval vars newStack expr //return to recursion on newStack
所以我的问题不一定是关于代码本身(除非有人能看到它有什么问题),而是关于错误消息:显然,这意味着我有一个类型不正确的元素,但是(1194,38)是什么意思?我假设第38行,但每次运行代码时,较大的数字都会增加,我不确定这意味着什么
抱歉,如果这是一个noob问题,那么我们就没有方向性地完成了这个任务 您的一个函数希望得到一个“字符串”(我喜欢把许多字符想象成一个字符串所包含的党字母),但得到的是一个“c”、“h”、“a”、“r”(单个字符)。我怀疑newvarlist是因为innereval看起来不错?不看函数很难说,但是你应该能够用鼠标悬停在它们上面。VisualStudio和带有ionide扩展的VisualStudio代码都支持这一点。你甚至会在不起作用的代码行上看到红色的曲线。让我知道这是否有帮助 F#的
T列表
是一个类型为T
的不可变单链表,不能与string
互换,后者是它自己的基本类型。但是,string
隐式地是seq
,这就是为什么可以使用类似seq.toList expr
的函数
因此,您将无法使用列表模式进行匹配
let rec innerEval (vars:(List<(string * int)>)) (stack: int list) (expr:string) =
match expr with //won't compile because expr isn't list
| [] -> stack.Head
| head::tail ->
innerEval vars newStack tail //won't compile because tail is char list
let rec innerEval(vars:(List))(stack:int List)(expr:string)=
将expr与//匹配将不会编译,因为expr不是列表
|[]->堆栈头
|头:尾->
innerEval vars newStack tail//无法编译,因为tail是char list
一个更简单的解决方案是将
innerEval(expr:string)
的签名更改为innerEval(expr:char list)
,这样转换就简化了。首先,noob问题通常是最好的问题,因为我们想让刚开始的人更容易!1194是控制台中的行,34是列。这就是为什么这个数字每次都变大,因为它也有你之前评估过的次数。我个人不喜欢这种默认设置,它对运行少量代码的人来说非常有效,但对于想运行整个程序的人来说,它可能是一个痛点。@Hailey Dice你的问题“我知道它很冗长”。比修复特定错误更重要的是知道如何创建用于诊断错误和提问的最少示例。这个答案说明了如何做到这一点。代码所做的是无关紧要的,因此不需要保留功能。如果你在保留错误的同时不断减少错误,你最终会得到这样的结果。
let rec innerEval (vars:(List<(string * int)>)) (stack: int list) (expr:string) =
match expr with //won't compile because expr isn't list
| [] -> stack.Head
| head::tail ->
innerEval vars newStack tail //won't compile because tail is char list