Skip to content

Instantly share code, notes, and snippets.

@akanehara
Created September 25, 2014 13:32
Show Gist options
  • Select an option

  • Save akanehara/ca234375b50799b06f98 to your computer and use it in GitHub Desktop.

Select an option

Save akanehara/ca234375b50799b06f98 to your computer and use it in GitHub Desktop.
末尾再帰 concat の2引数を組にしたもの
(* 目的: リストで与えられた文字列を連結する *)
let rec concat xs =
match xs with
[] -> ""
| first :: rest -> first ^ concat rest;;
(* 末尾再帰版の2つの引数を組にしたもの *)
(* インタプリタ上で #trace concat2;; してから実行してみましょう *)
let rec concat2 (xs, out) =
match xs with
[] -> out
| first :: rest -> concat2 (rest, out ^ first);;
(* テスト *)
let c1 = concat [] = "";;
let c2 = concat ["あ"] = "あ";;
let c3 = concat ["あ";"い";"う"] = "あいう";;
let c4 = concat2 ([], "") = "";;
let c5 = concat2 (["あ"], "") = "あ";;
let c6 = concat2 (["あ";"い";"う"], "") = "あいう";;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment