View Problem

Join the elements of a list, separated by commas

Given the list [Apple, Banana, Carrot] produce "Apple, Banana, Carrot"
ExpandDiskEdit
ruby
string = fruit.join(', ')
ExpandDiskEdit
erlang
Result = string:join(Fruit, ", "),
ExpandDiskEdit
erlang
Result = lists:foldl(fun (E, Acc) -> Acc ++ ", " ++ E end, hd(Fruit), tl(Fruit)),
ExpandDiskEdit
erlang
Result = lists:flatten([ hd(Fruit) | [ ", " ++ X || X <- tl(Fruit)]]).
DiskEdit
csharp .NET 3.5
using System.Collections.Generic;
public class JoinEach {
public static void Main() {
var list = new List<string>() {"Apple", "Banana", "Carrot"};
System.Console.WriteLine( string.Join(", ", list.ToArray()) );
}
}
ExpandDiskEdit
fsharp
let result = String.Join(", ", [|"Apple"; "Banana"; "Carrot"|])
ExpandDiskEdit
fsharp
let result = (List.fold_left (fun acc item -> acc ^ (", " ^ item)) (List.hd fruit) (List.tl fruit))
ExpandDiskEdit
fsharp
let result = (List.fold_left (fun (acc : StringBuilder) (item : string) -> acc.Append(", ").Append(item)) (new StringBuilder(List.hd fruit)) (List.tl fruit)).ToString()

Submit a new solution for ruby, erlang, csharp, or fsharp
There are 21 other solutions in additional languages (clojure, cpp, fantom, go ...)