View Problem

Perform an action multiple times based on a boolean condition, checked before the first action (WHILE .. DO)

Starting with a variable x=1, Print the sequence "1,2,4,8,16,32,64,128," by doubling x and checking that x is less than 150.
ExpandDiskEdit
fsharp
let mutable x = 1
while x < 150 do printf "%d, " x ; (x <- x * 2) done
DiskEdit
fsharp
// The problem is clearly geared towards imperative languages ;-)
// No need to mutate any variable, here's how to do it loop-free functional:
let rec powers2 i = seq { if i < 150 then yield i; yield! powers2 (i*2) }
powers2 1 |> Seq.iter (fun i -> printf "%i, " i)
ExpandDiskEdit
erlang
X = 1, print_while_X_less_150(X).
ExpandDiskEdit
erlang
Pred = fun (X) -> X < 150 end,
Action = fun (X) -> io:format("~B,", [X]), X * 2 end,
X = 1,

while_do(Pred, Action, X).
DiskEdit
clojure
(take-while #(< % 150) (iterate #(* 2 %) 1))
DiskEdit
groovy
x = 1
while (x < 150) {
print x + ","
x *= 2
}
println()

Submit a new solution for fsharp, erlang, clojure, or groovy
There are 14 other solutions in additional languages (cpp, csharp, fantom, haskell ...)