#『プログラミングの基礎』社内読書会 #0
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 |
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
| (* 日付を表現するレコード *) | |
| type date_t = { | |
| month: int; | |
| day: int; | |
| } | |
| (* 人物を表現するレコード *) | |
| type person_t = { | |
| name: string; | |
| height: float; |
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
| (* 日付を表現するレコード *) | |
| type date_t = { | |
| month: int; | |
| day: int; | |
| } | |
| (* 人物を表現するレコード *) | |
| type person_t = { | |
| height: float; | |
| weight: float; |
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
| (* 目的: 偶数の要素だけ抽出する *) | |
| (* even : int list -> int -> list *) | |
| let rec even xs = | |
| match xs with | |
| [] -> [] | |
| | first :: rest -> | |
| if first mod 2 = 0 | |
| then first :: even rest | |
| else even rest;; |
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
| (* 目的: 受け取ったリストの長さを返す関数length *) | |
| (* length : 'a list -> int *) | |
| let rec length xs = | |
| match xs with | |
| [] -> 0 | |
| | first :: rest -> 1 + length rest;; | |
| (* テスト *) | |
| let length1 = length [] = 0;; | |
| let length2 = length [1] = 1;; |
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
| (* 駅名ひとつ分のデータ ekimei_t *) | |
| type ekimei_t = { | |
| kanji : string; | |
| kana : string; | |
| romaji : string; | |
| shozoku : string; | |
| } | |
| (* 目的:駅名をきれいに表示する *) | |
| let hyoji e = |
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
| (* 日付 *) | |
| type date_t = { | |
| year : int; | |
| month : int; | |
| day : int; | |
| } | |
| (* 目的: お小遣い帳の情報を格納するレコード型 okozukai_t *) | |
| type okozukai_t = { | |
| title : string; |
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
| (* 目的: 本に関するレコード型 book_t を宣言する *) | |
| type book_t = { | |
| title : string; | |
| author : string; | |
| publisher : string; | |
| price : int; | |
| isbn : string; | |
| } | |
| let book1 = { |
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
| (* 目的: 中点 *) | |
| (* chuten : float * float -> float * float -> float * float *) | |
| let chuten p q = | |
| match (p, q) with | |
| ((px, py), (qx, qy)) -> | |
| ((px +. qx) /. 2., (py +. qy) /. 2.) | |
| (* テスト *) | |
| let chuten1 = chuten (1., 1.) (-1., -1.) = (0., 0.) | |
| let chuten2 = chuten (-1., 1.) (1., -1.) = (0., 0.) |
