Skip to content

Instantly share code, notes, and snippets.

@akanehara
akanehara / c0.md
Last active December 14, 2017 17:42
『プログラミングの基礎』社内読書会 #0

#『プログラミングの基礎』社内読書会 #0

処理系のインストール

Mac, Linux

@akanehara
akanehara / c9_6t.ml
Created September 25, 2014 13:32
末尾再帰 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
@akanehara
akanehara / c9_8.ml
Last active August 29, 2015 14:06
乙女座の人の名前のみからなるリストを返す関数
(* 日付を表現するレコード *)
type date_t = {
month: int;
day: int;
}
(* 人物を表現するレコード *)
type person_t = {
name: string;
height: float;
@akanehara
akanehara / c9_7.ml
Last active August 29, 2015 14:06
血液型がAの人物の数を返す関数 count_ketsueki_A
(* 日付を表現するレコード *)
type date_t = {
month: int;
day: int;
}
(* 人物を表現するレコード *)
type person_t = {
height: float;
weight: float;
@akanehara
akanehara / c9_5.ml
Created September 25, 2014 11:25
偶数の要素だけ抽出する even
(* 目的: 偶数の要素だけ抽出する *)
(* 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;;
@akanehara
akanehara / c9_4.ml
Created September 25, 2014 10:57
受け取ったリストの長さを返す関数length
(* 目的: 受け取ったリストの長さを返す関数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;;
@akanehara
akanehara / c8_6.ml
Last active August 29, 2015 14:06
きれいな駅名
(* 駅名ひとつ分のデータ ekimei_t *)
type ekimei_t = {
kanji : string;
kana : string;
romaji : string;
shozoku : string;
}
(* 目的:駅名をきれいに表示する *)
let hyoji e =
@akanehara
akanehara / okozukai_t.ml
Created September 18, 2014 10:17
お小遣い帳
(* 日付 *)
type date_t = {
year : int;
month : int;
day : int;
}
(* 目的: お小遣い帳の情報を格納するレコード型 okozukai_t *)
type okozukai_t = {
title : string;
@akanehara
akanehara / book_t.ml
Created September 18, 2014 10:01
本に関するレコード型 book_t
(* 目的: 本に関するレコード型 book_t を宣言する *)
type book_t = {
title : string;
author : string;
publisher : string;
price : int;
isbn : string;
}
let book1 = {
@akanehara
akanehara / chuten2.ml
Created September 11, 2014 11:11
別解
(* 目的: 中点 *)
(* 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.)