F #을 배우고 있고 수레 스택에서 수학 연산을 수행해야하는 연습을하고 있습니다.중첩 연산자를 매개 변수로 보낼 때 F # 형식이 일치하지 않습니다.
Type mismatch. Expecting a
float * float -> float
but given a
float * float -> 'a -> 'b
The type 'float' does not match the type ''a -> 'b'
이유 : 내가 인수로 전달하는 시도 (+, -, *, /)
exception InterpreterError;;
type Instruction =
| ADD
| SUB
| MULT
| DIV
| SIN
| COS
| LOG
| EXP
| PUSH of float;;
type Stack = S of float list;;
let pop (S(s)) =
match s with
| [] -> raise InterpreterError
| x::_ -> (x,S(s));;
let push x (S(s)) : Stack = S(x::s)
let applyBin f s : Stack =
let (first, sGen1) = pop s
let (second,sGen2) = pop sGen1
push (f(first,second)) sGen2;;
let applyUni f s : Stack =
let (first, sGen1) = pop s
push (f(first)) sGen1;;
let intpInstr i s =
match i with
| ADD -> applyBin (+) s
| SUB -> applyBin (-) s
| MULT -> applyBin (*) s
| DIV -> applyBin (/) s
| SIN -> applyUni sin s
| COS -> applyUni cos s
| LOG -> applyUni log s
| EXP -> applyUni exp s
| PUSH(r) -> push r s;;
그러나, 나는 중위 연산자의 마지막 기능 intpInstr에서 컴파일러 오류를 받고 있어요 연산자는 (+)가됩니다 : float -> float -> 'a ->'b? 대화 형 콘솔에서이 유형을 복제 할 수 없었습니다. 모든 도움을 주셨습니다.
트릭을 수행하는 것처럼 보였습니다. 감사! 방금 터프 팅을했다고 가정 했으므로 중개 연산자를 구현하는 방법을 배웠습니다. – Runekn