Hy,我正在尝试创建一个直方图,但我一直收到错误。
Histogram example:
input :[2;1;2;3;2;1;2;2;5]
output :[(2,5);(1,2);(3,1);(5,1)]
我的代码:
let rec count a ls = match ls with
|[] -> 0
|x::xs when x=a -> 1 + count a xs
|_::xs -> count a xs
let rec histo l = match l with
|[] -> []
|x :: xs -> [(x, count x l)] @ List.filter(fun x -> xs != x)histo xs;;
错误: 此函数具有类型('a - > bool) - > '列表 - > '列表它适用于太多的论点;也许你忘记了一个`;'。
答案 0 :(得分:1)
你快要结束了;) 一些提示:
答案 1 :(得分:0)
let rec count a = function
|[] -> 0
|x::xs -> if x=a then 1 + count a xs else count a xs
let rec histo = function
|[] -> []
|x::xs -> let l'=List.filter ((<>)x) xs in
[(x, 1+count x xs)] @ histo l'
;;
测试
# histo [2;1;2;3;2;1;2;2;5];;
- : (int * int) list = [(2, 5); (1, 2); (3, 1); (5, 1)]
或者
let rec histo = function
|[] -> []
|x::xs -> let l'=List.filter ((<>)x) xs in
[(x, 1+List.length xs - List.length l')] @ histo l'
;;