Last active
August 29, 2015 14:16
-
-
Save akimacho/963e3badaaceff9850e7 to your computer and use it in GitHub Desktop.
第8章問題
This file contains 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
(* === 第8章問題 === *) | |
(* --- 問題8.1 --- *) | |
(* 本に関する情報を表す型 *) | |
(* 0で始まるコードもあるようなので,*) | |
(* isbnの型は,stringのほうがいいようだ *) | |
(* フィールドの順番を変えてみる *) | |
(* ちなみにauthorをauthourにすると処理系が *) | |
(* Hint : Did you mean authour?と指摘してくれる *) | |
# #use "book.ml" ;; | |
type book_t = { | |
title : string; | |
author : string; | |
publisher : string; | |
price : int; | |
isbn : string; | |
} | |
val b1 : book_t = | |
{title = "プログラミングの基礎"; author = "浅井健一"; | |
publisher = "サイエンス社"; price = 2300; isbn = "4781911609"} | |
val b2 : book_t = | |
{title = "プログラミング言語C 第2版"; author = "K & R"; | |
publisher = "共立出版"; price = 2800; isbn = "4320026926"} | |
val b3 : book_t = | |
{title = "Programming Perl"; author = "Larry Wall"; | |
publisher = "O'Reilly Media"; price = 3776; isbn = "0596000278"} | |
(* --- 問題8.2 --- *) | |
# #use "koz.ml" ;; | |
type okozukai_t = { | |
goods : string; | |
price : int; | |
shop : string; | |
date : int * int; | |
} | |
val o1 : okozukai_t = | |
{goods = "うまい棒"; price = 10; shop = "駄菓子屋"; date = (2, 22)} | |
val o2 : okozukai_t = | |
{goods = "えんぴつ"; price = 100; shop = "文房具"; date = (12, 13)} | |
val o3 : okozukai_t = | |
{goods = "時計"; price = 2500; shop = "デパート"; date = (4, 7)} | |
(* --- 問題8.3 --- *) | |
# #use "person.ml" ;; | |
type person_t = { | |
name : string; | |
height : float; | |
weight : float; | |
birth_day : int * int; | |
blood : string; | |
} | |
val o1 : person_t = | |
{name = "hoge"; height = 180.; weight = 80.; birth_day = (1, 1); | |
blood = "A"} | |
val o2 : person_t = | |
{name = "foo"; height = 150.; weight = 35.; birth_day = (3, 17); | |
blood = "AB"} | |
val o3 : person_t = | |
{name = "piyo"; height = 170.; weight = 60.; birth_day = (10, 28); | |
blood = "O"} | |
(* --- 問題8.4 --- *) | |
(* ketsueki.ml *) | |
(* 各人の名前・身長・誕生日・血液型を表す型 *) | |
type person_t = { | |
name : string; (* 名前 *) | |
height : float; (* 身長 *) | |
weight : float; (* 体重 *) | |
birth_day : int * int; (* 誕生日 *) | |
blood : string; (* 血液型 *) | |
} | |
(* 目的:person_t型のデータを受け取ったら,*) | |
(* "OOさんの血液型は△型です"という文字列を返す *) | |
(* ketsueki_hyoji : person_t -> string *) | |
let ketsueki_hyoji p = match p with | |
{ | |
name = n; | |
height = h; | |
weight = w; | |
birth_day = bd; | |
blood = bl; | |
} -> n ^ "さんの血液型は" ^ bl ^ "型" ^ "です" | |
(* テスト *) | |
let test1 = ketsueki_hyoji { | |
name = "hoge"; | |
height = 180.; | |
weight = 80.; | |
birth_day = (1, 1); | |
blood = "A"; | |
} = "hogeさんの血液型はA型です" | |
let test2 = ketsueki_hyoji { | |
name = "foo"; | |
height = 150.; | |
weight = 35.; | |
birth_day = (3, 17); | |
blood = "AB"; | |
} = "fooさんの血液型はAB型です" | |
let test3 = ketsueki_hyoji { | |
name = "piyo"; | |
height = 170.; | |
weight = 60.; | |
birth_day = (10, 28); | |
blood = "O"; | |
} = "piyoさんの血液型はO型です" | |
# #use "ketsueki.ml" ;; | |
type person_t = { | |
name : string; | |
height : float; | |
weight : float; | |
birth_day : int * int; | |
blood : string; | |
} | |
val ketsueki_hyoji : person_t -> string = <fun> | |
val test1 : bool = true | |
val test2 : bool = true | |
val test3 : bool = true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment