我尝试修改列表时遇到问题。我有一个(string*float) list
,我必须通过以下方式更改它:
1. If I find an existing string in the list, i have to change his value
2. Else I have to add the pair string(name) * float (value) to the list (don't care the position)
我向你展示了错误的代码:
exception Not_found;;
let rec searchMake l var = match l with
[]-> raise Not_found
| (h,x)::t-> if h=var then (h,x)
else (h,x)::(searchMake t var);;
let make varlist (var,value) =
try
let p=searchMake varlist var in
snd p <- value
with Not_found -> varlist <- (var, value)::varlist;;
答案 0 :(得分:1)
列表,元组和变量在OCaml中是不可变的,因此您无法使用<-
分配它们。相反,编写一个函数,返回一个具有所需属性的新列表。