Created
September 25, 2014 13:32
-
-
Save akanehara/ca234375b50799b06f98 to your computer and use it in GitHub Desktop.
末尾再帰 concat の2引数を組にしたもの
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (* 目的: リストで与えられた文字列を連結する *) | |
| 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