h::t -> if h = n then (incr counter; occ t n) else occ t n;;

问题描述 投票:0回答:1
(

Testl)Nocc l n;;NI hope this helps !l

let l = [1;2;3;4;5;6;7;1;9;10;11];;
let n = 1;;

nYou can sort your list and after that enumerate elements of it with calculating the repetition count for each number. This algorithm will have O (n*log(n)).lBetter O is possible only with the mutable state using. In the current case, it will be hashtable.

for i = 0 to (List.length l) do
(* codes here: i want to find that the value of N is present twice in the list *)
done;;
list ocaml ocamllex ocamlyacc ocamlfind
1个回答
0
投票

我是OCmal的初学者。我有一个名为 和一个存储在

.我需要找到的次数

重复 . 我想写一个函数,它可以告诉你次数

在于

.我开始是这样的,但是我被封了。

我是OCmal的初学者. 我有一个名为l的列表和一个存储在N中的数字,我需要找到N在l中重复的次数。让l = [1;2;3;4;5;6;7;1;9;10;11];; 让n = 1;; 我想写一个 ...(宣布名单l

)

让l=[1;2;3;4;5;6;7;1;9;10;11];。(宣布常数n为1

)

让n=1;。(声明变量(计数器)初始化为0。

)

让 counter = ref 0;;(声明递归函数( occ),以一个 list 和一个 int 为参数。

)

让rec occ l n = match l with

(

0
投票

)

let of_times n l = 
  match List.sort Int.compare l with
  | []     -> []
  | h :: t -> 
      let rec f current count = function
        | h :: t when h = current -> f h (succ count) t
        | h :: t when n = count   -> current :: (f h 1 t)
        | h :: t                  -> f h 1 t
        | []                      -> [] in
      f h 1 t

[] -> Printf.printf "列表中%d的出现次数为:%d/d/n" n !counter!

(

0
投票

)List.fold

© www.soinside.com 2019 - 2024. All rights reserved.