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
fib(0, 0). | |
fib(1, 1). | |
fib(X, Y1 + Y2) :- fib(X - 1, Y1), fib(X - 2, Y2). | |
%% これだと X - 1 がいつまでも簡約(?) | |
%% されずに停まらなくなるのどうして? | |
% {trace} | |
% | ?- fib(2, What). | |
% 1 1 Call: fib(2,_279) ? | |
% 2 2 Call: fib(2-1,_311) ? | |
% 3 3 Call: fib(2-1-1,_341) ? |
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
(* seiza int -> string *) | |
let seiza md = | |
if md < 0120 then | |
"牡羊座" | |
else if md <= 0218 then | |
"水瓶座" | |
else if md <= 0320 then | |
"魚座" | |
else if md <= 0419 then | |
"牡羊座" |
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
(* string * string -> string *) | |
let seiseki name_seiseki_pair = | |
match name_seiseki_pair with | |
(n, s) -> n ^ "さんの評価は" ^ s ^ "です" | |
let seiseki1 = seiseki ("カネハラ", "最悪") = "カネハラさんの評価は最悪です" | |
let seiseki2 = seiseki ("山田", "最高") = "山田さんの評価は最高です" | |
let seiseki3 = seiseki ("鈴木", "普通") = "鈴木さんの評価は普通です" |
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
(* 対称 *) | |
(* float * float -> (float * float) *) | |
let taisho_x p = | |
match p with | |
(x, y) -> (x, -. y) | |
let taisho_x1 = taisho_x (10., 20.) = (10., -20.) | |
let taisho_x2 = taisho_x (8., -5.2) = (8., 5.2) | |
let taisho_x3 = taisho_x (-10.5, 20.1) = (-10.5, -20.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
(* 目的: 中点 *) | |
(* chuten : float * float -> float * float -> float * float *) | |
let chuten p q = | |
match p with | |
(px, py) -> | |
match q with | |
(qx, qy) -> | |
((px +. qx) /. 2., (py +. qy) /. 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
(* 目的: 中点 *) | |
(* 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.) |
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
(* 日付 *) | |
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
(* 駅名ひとつ分のデータ 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
(* 目的: 受け取ったリストの長さを返す関数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;; |