2

Given a list of string elements in Python, I can concatenate these elements with specified glue. For example:

' '.join(["phd", "in", "everything"])

evaluates to the string "phd in everything".

What analogue in OCaml is considered most idiomatic? That is, how may string elements of a list be joined with some specified string in OCaml?

2 Answers 2

5

The equivalent in Ocaml is :

#String.concat " " ["phd"; "in"; "everything"]

There is no restriction vs the length of the list.

Sign up to request clarification or add additional context in comments.

Comments

0

The simple answer is best for this simple example, but if you're then taking the resulting string and immediately printing it, it may be more efficient to utilize the Format module and its pp_print_list function.

E.g.

# Format.(
    let lst = ["phd"; "in"; "everything"] in
    let pp_sep fmt () = fprintf fmt " " in
    asprintf "%a" (pp_print_list ~pp_sep pp_print_string) lst
  );;
- : string = "phd in everything"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.