簡體   English   中英

OCaml中的標簽~f

[英]label ~f in OCaml

我在OCaml工作,我使用的環境是Eclipe Mars。 當我嘗試使用List.iter [1.;3.;2.;-7.;4.;5.] ~f:(fun x -> update rsum x);; 它給出了一個錯誤,說Error: The function applied to this argument has type 'a list -> unit This argument cannot be applied with label ~f#重點是這是一本書中的一個例子,它完美地適用於他們但我經常當我嘗試使用~f:時出現此錯誤。 有人可以解釋一下為什么這不起作用? 如果我知道如何使這項工作,它也會有所幫助。

代字號引入了稱為標記參數的特征。 OCaml標准庫具有模塊List,其中聲明了沒有標記參數的函數和使用它們的模塊ListLabel。

我的假設是,當作者使用另一個名為Core standard庫時,您正在閱讀Real World OCaml。 這個庫有它自己的List模塊,這個庫經常使用帶標簽的參數。 所以,您可能忘了將此庫加載到頂層。 您可能需要解釋Eclispe如何加載正確的庫或在源文件的頂部寫入module List=ListLabels

標准List模塊不支持帶標簽的參數。 標准ListLabels模塊支持標記參數。

# List.iter ~f: (fun x -> Printf.printf "%d\n" x) [1; 2; 3];;
Error: The function applied to this argument has type 'a list -> unit
This argument cannot be applied with label ~f

# ListLabels.iter ~f: (fun x -> Printf.printf "%d\n" x) [1; 2; 3];;
1
2
3
- : unit = ()

如果您正在學習教程或書籍,則需要確保使用相同的模塊。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM