我碰到過這個函數:List.map
。我所理解的是List.map
將函數和列表作爲參數並轉換列表中的每個元素。OCaml中List.iter和List.map函數的區別
List.iter
做類似的東西(也許?),以供參考見下文:
# let f elem =
Printf.printf "I'm looking at element %d now\n" elem in
List.iter f my_list;;
I'm looking at element 1 now
I'm looking at element 2 now
I'm looking at element 3 now
I'm looking at element 4 now
I'm looking at element 5 now
I'm looking at element 6 now
I'm looking at element 7 now
I'm looking at element 8 now
I'm looking at element 9 now
I'm looking at element 10 now
- : unit =()
的例子有人可以解釋List.map
和List.iter
之間的區別?
注:我是OCaml和函數式編程的新手。
明白了。謝謝。 –