Scanf.scanf "%d" (fun n -> let rec loop i head tail = if i = n then head, tail else let head, tail = Scanf.scanf " %d %c" (fun t c -> if t = 0 then head, c :: tail else c :: head, tail ) in loop (i + 1) head tail in let h, t = loop 0 [] [] in let show l = List.iter (print_char) l in show h; show (List.rev t); print_newline () )