ocaml代码连续列表创建简单代码

时间:2017-03-13 22:22:06

标签: ocaml

我一直在尝试99个ocaml问题中的一个问题,你必须在列表中列出所有连续数字,例如[2; 3; 4; 4; 5; 6; 6; 6] - > [[2]; [3]; [4; 4]; [5] [6; 6; 6]]

let rec tail = function
     | [] -> []
     | [x] -> [x]
     | x::xs -> tail xs;;

let pack lst =
    let rec aux current acc = function
        | [] -> current
        | [x] -> if ((tail acc)=x) then ((x::acc)::current) else ([x]::current)
        | x::y::xs -> if (x=y) then aux (current) (x::acc) (y::xs) else aux (acc::current) ([]) (y::xs)
in
aux [] [] lst;;

当我运行时,我得到错误

错误:此表达式的类型为“列表
”                                                                                                                                                                                                            但是表达式是'列表清单的类型                                                                                                                                                                                       类型变量'a出现在'列表

我想知道问题是什么? 感谢任何帮助,它将受到极大的重视

1 个答案:

答案 0 :(得分:0)

正如Bergi指出ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { // pre-condition return; } int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom(); int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.AT_MOST); Log.d("list", "" + listAdapter.getCount()); for (int i = 0; i < listAdapter.getCount(); i++) { View listItem = listAdapter.getView(i, null, listView); if (listItem != null) { // This next line is needed before you call measure or else you won't get measured height at all. The listitem needs to be drawn first to know the height. listItem.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT)); listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED); totalHeight += listItem.getMeasuredHeight(); Log.d("total height", "" + totalHeight); } } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); Log.d("params height", "" + params.height); listView.setLayoutParams(params); listView.requestLayout(); 是问题所在。 tail返回(tail acc)=x,因此x也必须是'a list类型。以下'a list然后推断acc必须是x::acc类型。但'a list list推断为tail acc

此时ocaml无法统一'a list'a list list的tpyes并给出您看到的错误。

相关问题