小编otc*_*com的帖子

OCaml此函数适用于太多参数

这只是我编写的一个简单程序,试图更好地理解模块.我正在尝试调用该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)

ocaml module

3
推荐指数
1
解决办法
3205
查看次数

如何在Ocaml中将非内置类型转换为字符串?

我正在尝试编写一个模块来转换类似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

2
推荐指数
1
解决办法
5399
查看次数

OCaml错误失败"hd"

我尝试编写一个简单的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"".不确定为什么.谢谢!!

ocaml functional-programming

1
推荐指数
1
解决办法
2060
查看次数

标签 统计

ocaml ×3

functional-programming ×1

module ×1