我想为ocaml中的链表创建一个递归类型,我为其定义了列表元素的类型。我写了以下代码
GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2, GridLayoutManager.HORIZONTAL, false);
RecyclerView recyclerView = findViewById(R.id.DicesGridView);
recyclerView.setLayoutManager(gridLayoutManager);
DicesElementAdapter adapter = new DicesElementAdapter(this, DiceList, Result, LogsList, gson, sharedPreference);
recyclerView.setAdapter(adapter);
但我收到语法错误。如何纠正错误?
答案 0 :(得分:5)
2016年4月,OCaml 4.03引入了这种语法("内联记录作为数据类型构造函数的参数"):http://ocaml.org/releases/4.03.html
有些系统,比如我的Ubuntu,包含旧版本的OCaml,它不支持此功能:
$ /usr/bin/ocaml
OCaml version 4.02.3
# type elem=
|Nil
|Elem of {content:int;mutable next:elem};;
Error: Syntax error
解决此问题的最佳方法是安装较新版本的OCaml并保持更新,最好使用OPAM:https://opam.ocaml.org/doc/Install.html
如果这不是一个选项,你可以通过使用单个相互递归的定义(type t1 = ... and t2 = ...
)声明代数数据类型和记录类型以及相同的时间来解决它:
# type elem = Nil | Elem of elem_record
and elem_record = { content: int; mutable next: elem };;
type elem = Nil | Elem of elem_record
and elem_record = { content : int; mutable next : elem; }