我有一個程序循環一個字符串並打印出每個字符的dir
。在OCaml中總計一個變量
我想將這些dir
值合計成一個單獨的離散值,我可以打印。
這裏是我當前的代碼:
let word = "(())";;
let dir = function
| '(' -> 1
| ')' -> -1
| _ -> 0;;
let z =
(String.iter (fun (c: char) ->
let d = dir c in
Printf.printf "%d is the direction of %c\n" d c
) word);;
目前,這樣就會打印出以下幾點:
1 is the direction of (
1 is the direction of (
-1 is the direction of)
-1 is the direction of)
而不是打印這些了,我想它總結了4個值( 1,-1,-1,-1)並打印出來:
The overall direction is 0
如何在OCaml中實現此目的?
...所以String.fold存在!很高興知道 ;) –