这只是我编写的一个简单程序,试图更好地理解模块.我正在尝试调用该toS函数,Id("a",Int)但似乎我可以编写类似这样的类型.可能是什么问题?
module Typ =
struct
type typ = Int | Bool
end
module Symbol =
struct
type t = string
end
module Ast =
struct
type ast = Const of int * Typ.typ | Id of Symbol.t * Typ.typ
let rec toS ast = match ast with Id(a,b) -> "a"
|_->"b"
end
Ast.toS Id("a",Int)
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个模块来转换类似x:int = if true,然后将3 else 5转换为字符串
这是我到目前为止的代码
module Ast =
struct
type typ = Bool | Int
type var = A | B | C | D | E | F
type exp = Const of int * typ
| App of string * exp list
| If of exp * exp * exp
| And of exp * exp
| Or of exp * exp
| Id of var * typ * exp
let rec toString (t) =
let …Run Code Online (Sandbox Code Playgroud) 我尝试编写一个简单的OCaml程序,如果列表包含所有偶数整数则返回true,否则返回false.
let rec allEven l =
List.hd l mod 2 = 0 && allEven (List.tl l);;
Run Code Online (Sandbox Code Playgroud)
当我输入代码时,它没有给我任何错误.但每当我输入一个以allEven [2; 3]之类的偶数开头的列表时,它会给出错误消息"Failure"hd"".不确定为什么.谢谢!!