Comment detail

与えられた文字列でピラミッド (Nested Flatten)
みなさんの投稿を参考にしつつ初投稿。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
let pyramid str =
  let rec letters = function
    | "" -> []
    | s  -> (String.sub s 0 1) :: (letters (String.sub s 1 (String.length s - 1)))
  in
  fst (List.fold_right (fun s t -> let x = s ^ snd t in (x :: (fst t), " " ^ x))
                       (letters str) ([], ""))
  |> List.mapi (fun i s -> (String.make i ' ') ^ s) |> List.rev |> String.concat "\n";;

let _ = pyramid "abracadabra" |> printf "%s\n";;

Index

Feed

Other

Link

Pathtraq

loading...