此表达式的类型为 float,但表达式应为 float 选项类型
This expression has type float but an expression was expected of type float option
嗨,我需要准备考试,我们的老师给了我们一些作业要做,这是我的问题,我有一个错误我无法解决。我们正在研究算术表达式,这是我的类型:
type expr_a = Val of float
|Var of string
|Plus of expr_a * expr_a
|Moins of expr_a * expr_a
|Div of expr_a * expr_a
|Mult of expr_a * expr_a
|Umoins of expr_a
|Puiss of float * float
|Sin of expr_a
|Cos of expr_a
|Exp of expr_a
|Ln of expr_a ;;
我已经做了一个显示有效算术表达式的函数,但现在他给了我们一个我们需要使用的函数
let valeuropt v =
if v = "x" then Some(0.)
else if v = "y" then Some(1.)
else None;;
当我尝试使用它时出现错误:"This expression has type float but an expression was expected of type"
浮动选项
let rec partielle expr = match expr with
Var x -> valeuropt x
|Val x -> x
|Plus (e1,e2) -> (evaluer e1 +. evaluer e2)
|Moins (e1,e2) -> (evaluer e1 -. evaluer e2)
|Div (e1,e2) -> (evaluer e1 /. evaluer e2)
|Mult (e1,e2) -> (evaluer e1 *. evaluer e2)
|Puiss(a,b) -> a**b
|Sin(e) -> sin(evaluer e)
|Cos(e) -> cos(evaluer e)
|Exp(e) -> exp(evaluer e)
|Ln(e) -> log(evaluer e)
|Umoins(e) -> -. evaluer e;;
如您所见,valeuropt
return 是一个 float option
类型的值,基本上表示 "maybe a value containing a float, maybe a value containing nothing".
但是,在您的代码中,您将其视为 return 与 float option
.
不兼容的 float
我猜你的老师希望你提供一个 partielle
函数,它将 return 一个 float option
(取决于它对禁止变量的使用)。
后面的函数(以及其他类似的函数)可能对该任务有所帮助:
let map_opt f x =
match x with
| None -> None
| Some y -> Some (f y)
嗨,我需要准备考试,我们的老师给了我们一些作业要做,这是我的问题,我有一个错误我无法解决。我们正在研究算术表达式,这是我的类型:
type expr_a = Val of float
|Var of string
|Plus of expr_a * expr_a
|Moins of expr_a * expr_a
|Div of expr_a * expr_a
|Mult of expr_a * expr_a
|Umoins of expr_a
|Puiss of float * float
|Sin of expr_a
|Cos of expr_a
|Exp of expr_a
|Ln of expr_a ;;
我已经做了一个显示有效算术表达式的函数,但现在他给了我们一个我们需要使用的函数
let valeuropt v =
if v = "x" then Some(0.)
else if v = "y" then Some(1.)
else None;;
当我尝试使用它时出现错误:"This expression has type float but an expression was expected of type" 浮动选项
let rec partielle expr = match expr with
Var x -> valeuropt x
|Val x -> x
|Plus (e1,e2) -> (evaluer e1 +. evaluer e2)
|Moins (e1,e2) -> (evaluer e1 -. evaluer e2)
|Div (e1,e2) -> (evaluer e1 /. evaluer e2)
|Mult (e1,e2) -> (evaluer e1 *. evaluer e2)
|Puiss(a,b) -> a**b
|Sin(e) -> sin(evaluer e)
|Cos(e) -> cos(evaluer e)
|Exp(e) -> exp(evaluer e)
|Ln(e) -> log(evaluer e)
|Umoins(e) -> -. evaluer e;;
如您所见,valeuropt
return 是一个 float option
类型的值,基本上表示 "maybe a value containing a float, maybe a value containing nothing".
但是,在您的代码中,您将其视为 return 与 float option
.
float
我猜你的老师希望你提供一个 partielle
函数,它将 return 一个 float option
(取决于它对禁止变量的使用)。
后面的函数(以及其他类似的函数)可能对该任务有所帮助:
let map_opt f x =
match x with
| None -> None
| Some y -> Some (f y)