I'd like to do something like this
String.concat '\n' [str1; str2 ... strn]
so I can print in a file. But ocaml doesn't allow me to do that. What can I do?
I'd like to do something like this
String.concat '\n' [str1; str2 ... strn]
so I can print in a file. But ocaml doesn't allow me to do that. What can I do?
String.concat "\n" [str1; str2 ... strn]
works fine. The problem is that you used '\n'
, which is a character literal, not a string. Example:
# String.concat '\n' ["abc"; "123"];;
Error: This expression has type char but an expression was expected of type
string
# String.concat "\n" ["abc"; "123"];;
- : string = "abc\n123"
If you're using Jane Street's base module for your standard library you'll have to do it like so:
# #require "base";;
# open! Base;;
# String.concat ~sep:"\n" ["abc"; "123"];;
- : string = "abc\n123"
Jane Street really likes to take advantage of named arguments.
© 2022 - 2024 — McMap. All rights reserved.
String.concat
print "abc" and "123" on two different lines`? – Radu