functional programming - Error in using fold_left in OCaml -
i trying convert integers in list corresponding ascii values , concatenating them form string. tried:
# let l = [65;66;67];; # list.fold_left (fun x y -> char_of_int x ^ char_of_int y) "" l;;
i getting following error :
error: expression has type char expression expected of type string
marking char_of_int x
error.
the error occurs, because ocaml operator ^
accept 2 strings, cannot directly concatenate 2 characters. in order build string, first have convert individual characters strings (of length 1). can concatenate these short strings.
# let chars = list.map char_of_int l;; val chars : char list = ['a'; 'b'; 'c'] # let strings = list.map (string.make 1) chars;; val strings : string list = ["a"; "b"; "c"] # string.concat "" strings;; - : string = "abc"
Comments
Post a Comment