読者です 読者をやめる 読者になる 読者になる

個人的自由ノート

ふと自分が気になった事を書いてます

OCamlの練習

練習問題、問題の意味がわからなくて少し苦戦しましたが、ネットで調べて解決しました。インタプリタで日本語を扱うのも少々複雑なので、調べてやってみました。

        OCaml version 4.02.3

# (1.0, 2.0);;
- : float * float = (1., 2.)
# let author = ("Atsushi", "Igarashi", 174.0, 61.0);; 
val author : string * string * float * float =
  ("Atsushi", "Igarashi", 174., 61.)
# let (firstname, lastname, height, weight) = author;;
val firstname : string = "Atsushi"
val lastname : string = "Igarashi"
val height : float = 174.
val weight : float = 61.
# let (x, y) = big_tuple;;
Error: Unbound value big_tuple
# let big_tuple = ((3,'a'), (9.3, "Hello", false));;
val big_tuple : (int * char) * (float * string * bool) =
  ((3, 'a'), (9.3, "Hello", false))
# let (x, y) = big_tuple;;
val x : int * char = (3, 'a')
val y : float * string * bool = (9.3, "Hello", false)
# let (f, s, b) = y;;
val f : float = 9.3
val s : string = "Hello"
val b : bool = false
# let (x, (f, s, b)) = big_tuple;;
val x : int * char = (3, 'a')
val f : float = 9.3
val s : string = "Hello"
val b : bool = false
# let ((i, c), (f, s, b)) = big_tuple;;
val i : int = 3
val c : char = 'a'
val f : float = 9.3
val s : string = "Hello"
val b : bool = false
# let (first, last, _, _) = author;;
val first : string = "Atsushi"
val last : string = "Igarashi"
# let (_, (_, s, _)) = big_tuple;;
val s : string = "Hello"
# let (_,_,h,h) = author;;
Error: Variable h is bound several times in this matching
# let average p =
  let (x,y) = p in
  (x +. y) /. 2.0;;
val average : float * float -> float = <fun>
# average(2.5, 4.8);;
- : float = 3.65
# let average (x, y) = (x +. y) /. 2.0;;
val average : float * float -> float = <fun>
# let pair = (0.34, 1.2);;
val pair : float * float = (0.34, 1.2)
# average pair;;
- : float = 0.77
# let sum_and_diff (x, y) = (x + y, x - y);;
val sum_and_diff : int * int -> int * int = <fun>
# let geo_mean (x, y) = sqrt(x * y);;
Error: This expression has type int but an expression was expected of type
         float
# let geo_mean (x, y) = sqrt(x *. y);;
val geo_mean : float * float -> float = <fun>
# geo_mean 3.4 5.6;;
Error: This function has type float * float -> float
       It is applied to too many arguments; maybe you forgot a `;'.
# geo_mean(3.4, 5.7);;
- : float = 4.40227214061102767
# let bmi (name, height, weight) = let ans = weight / height * height in                   
  if ans < 18.5 then name ^ "さんは痩せています"
  else if ans >= 18.5 && ans < 25 then name ^ "さんは標準です"
  else if ans >= 25 && ans < 30 then name ^ "さんは肥満です"
  else name ^ "さんは高度肥満です。";;
Error: This expression has type float but an expression was expected of type
         int
# let bmi (name, height, weight) = let ans = weight /. height *. height in
  if ans < 18.5 then name ^ "さんは痩せています"
  else if ans >= 18.5 && ans < 25. then name ^ "さんは標準です"
  else if ans >= 25. && ans < 30. then name ^ "さんは肥満です"
  else name ^ "さんは高度肥満です";;
val bmi : string * float * float -> string = <fun>
# bmi();;    
- : unit = ()
# bmi("テスト", 170, 140);;
Error: This expression has type int but an expression was expected of type
         float
# bmi("テスト", 170., 140.);;
- : string =
"\227\131\134\227\130\185\227\131\136\227\129\149\227\130\147\227\129\175\233\171\152\229\186\166\232\130\165\230\186\128\227\129\167\227\129\153"
# let print_non_escaped_string ppf = Format.fprintf ppf "\"%s\"";;
val print_non_escaped_string : Format.formatter -> string -> unit = <fun>
# #install_printer print_non_escaped_string;;
# let bmi (name, height, weight) = let ans = weight /. (height *. height) in
  if ans < 18.5 then name ^ "さんは痩せています"
  else if ans >= 18.5 && ans < 25. then name ^ "さんは標準です"
  else if ans >= 25. && ans < 30. then name ^ "さんは肥満です"
  else name ^ "さんは高度肥満です";;
val bmi : string * float * float -> string = <fun>
# bmi("太郎", 180.,75.);;
- : string = "太郎さんは痩せています"
# bmi("三郎", 160.,55.);;
- : string * float * float = ("三郎", 160., 55.)
# bmi("三郎", 170.,34.);;
- : string = "三郎さんは痩せています"
# bmi("四郎", 180.,33.);;
- : string = "四郎さんは痩せています"
# let sum_and_diff (x, y) = (x + y, x - y);;
val sum_and_diff : int * int -> int * int = <fun>
# let f (x, y) = sum_and_diff(x, y);;
val f : int * int -> int * int = <fun>
# f(1,2);;
- : int * int = (3, -1)
# let f (sum_and_diff(x, y)) = (x, y);;
Syntax error: ')' expected, the highlighted '(' might be unmatched
# let f sum_and_diff(x, y) = (x, y);;
val f : 'a -> 'b * 'c -> 'b * 'c = <fun>
# f(1,2);;
- : '_a * '_b -> '_a * '_b = <fun>
# let f (x, y) = sum_and_diff(x, y);;
val f : int * int -> int * int = <fun>
# f(1, 2);;
- : int * int = (3, -1)
# let f (x, y) = ((x + y) / 2, (x - y) / 2);;
val f : int * int -> int * int = <fun>
# f(sum_and_diff(x, y));;
Error: This expression has type int * char
       but an expression was expected of type int
# f(sum_and_diff(1,2));;
- : int * int = (1, 2)

タプル辺りから少々難しくなってきましたね。数学のスキルが少し足りないのを感じるかも…。