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

個人的自由ノート

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

OCaml 今日の成果

OCaml面白いです。この分だとHaskellも楽しみですね。

参考書を見ながら実行結果を確認し、問題を解いてみました。大雑把に成果を載せてみます。

# let pi = 3.1415926535
  let area_of_circle r = r *. r *. pi;;
val pi : float = 3.1415926535
val area_of_circle : float -> float = <fun>
# area_of_circle 2.0;;
- : float = 12.566370614
# let pi = 3.1415926535
  let area_of_circle r = r *. r *. pi
  let area_of_circle(r:float):float = r *. r *. pi;;
val pi : float = 3.1415926535
val area_of_circle : float -> float = <fun>
# area_of_circle 2.0;;
- : float = 12.566370614
# let abs x = if x > 0 then x else -x;;
val abs : int -> int = <fun>
# abs 5;;
- : int = 5
# abs -5;;
Error: This expression has type int -> int
       but an expression was expected of type int
# abs (-5);;
- : int = 5
# let area_of_circle r = if r > 0.0 then r *. r *. pi else 0.0;;
val area_of_circle : float -> float = <fun>
# 3 > 0
  ;;
- : bool = true
# 4.0 > 5.3;;
- : bool = false
# true;;
- : bool = true
# let is_positive x = x > 0;;
val is_positive : int -> bool = <fun>
# is_positive 3;;
- : bool = true
# is_positive (-9);;
- : bool = false
# 3.2 >= 5.1;;
- : bool = false
# infinity = infinity;;
- : bool = true
# nan = nan;;
- : bool = false
# 'a' < 'Z';;
- : bool = false
# "This" > "That";;
- : bool = true
# "Boo" < "Boole";;
- : bool = true
# true > false;;
- : bool = true
# "This" >= 'T';;
Error: This expression has type char but an expression was expected of type
         string
# let is_teenager x = 13 <= x && x < 20;;
val is_teenager : int -> bool = <fun>
# is_teenager 18;;
- : bool = true
# is_teenager 12;;
- : bool = false
# (not (1 < 2)) || (4.2 <> 4.2);;
- : bool = false
# let vol_cone = 
  let base = pi *. 2.0 *. 2.0 in
  base *. 5.0 /. 3.0;;
val vol_cone : float = 20.9439510233333337
# let vol_cone =
  let base = pi *. 2.0 *. 2.0 in
  base *. 5.0 /. 3.0;;
val vol_cone : float = 20.9439510233333337
# let cone_of_heightTwo r = 
  let base = r *. r *. pi in
  base *. 2.0 /. 3.0;;
val cone_of_heightTwo : float -> float = <fun>
# let f x = 
  let x3 = x * x * x in
  let x3_1 = x3 + 1 in
  x3 + x3_1;;
val f : int -> int = <fun>
# let g x = 
  let power3 x = x * x * x in
  (power3 x) + (power3 (x + 1));;
val g : int -> int = <fun>
# let power3 x = x * x * x;;
val power3 : int -> int = <fun>
# let g x = (power3 x) + (power3 (x + 1));;
val g : int -> int = <fun>
# let one = 1;;
val one : int = 1
# let two = one + one;;
val two : int = 2
# let one = "One";;
val one : string = "One"
# let three = one ^ one ^ one;;
val three : string = "OneOneOne"
# let pi = 3.1415926535;;
val pi : float = 3.1415926535
# let area_c r = r *. r *. pi;;
val area_c : float -> float = <fun>
# let pi = 1;;
val pi : int = 1
# let area = area_c 2.0;;
val area : float = 12.566370614
# let x = 2 and y = 1;;
val x : int = 2
val y : int = 1
# let x = y and y = x;;  
val x : int = 1
val y : int = 2
# let z =
  let f x = "foo" ^ x
  and y = 3.1 in
  f (string_of_float y);;
val z : string = "foo3.1"
# let convert us = 
  let res = us * 114.32 in floor res;;
Error: This expression has type float but an expression was expected of type
         int
# let convert us =
  let res = us *. 114.32 in floor res;;
val convert : float -> float = <fun>
# convert 1;;
Error: This expression has type int but an expression was expected of type
         float
# convert 1.0;;
- : float = 114.
# let convert2 yen =
  let res = yen /. 114.32 in floor res;;
val convert2 : float -> float = <fun>
# convert2 100;;
Error: This expression has type int but an expression was expected of type
         float
# convert2 100.0;;
- : float = 0.
# convert2 1000.;;
- : float = 8.
# let convert us =
  let res = us *. 114.32 in floor (res +. 0.5);;
val convert : float -> float = <fun>
# convert 1.;;
- : float = 114.
# convert 20.;;
- : float = 2286.
# let convert2 yen =
  let res = yen /. 114.32 in floor (res +. 0.5);;
val convert2 : float -> float = <fun>
# convert2 100;;
Error: This expression has type int but an expression was expected of type
         float
# convert2 100.;;
- : float = 1.
# let message us =
  let res_m = " " ^ string_of_float(us) ^ " dollars are " ^ string_of_float(convert2(us)) ^ " yen.";;
Error: Syntax error
# let message us =
  let yen = convert2(us) in " " ^ string_of_float(us) ^ " dollars are " ^ string_of_float(yen) ^ " yen.";;
val message : float -> string = <fun>
# message 1.;
  ;
  ;;
Error: Syntax error
# message 1.;;
- : string = " 1. dollars are 0. yen."
# let message us =
  let yen = convert(us) in " " ^ string_of_float us ^ " dollars are " ^ string_of_float yen ^ " yen.";;
val message : float -> string = <fun>
# message 1.;;
- : string = " 1. dollars are 114. yen."
# let capitalize target =
  uppercase target;;        
Error: Unbound value uppercase
# let capitalize target = uppercase(target);;
Error: Unbound value uppercase
# let capitalize target = String.uppercase target;;
Warning 3: deprecated: String.uppercase
Use String.uppercase_ascii instead.
val capitalize : string -> string = <fun>
# capitalize 'h';;
Error: This expression has type char but an expression was expected of type
         string
# capitalize "h";;
- : string = "H"
# let capitalize target = char_of_string (String.uppercase target);;
Error: Unbound value char_of_string
# let capitalize target = Char.uppercase target;;
Warning 3: deprecated: Char.uppercase
Use Char.uppercase_ascii instead.
val capitalize : char -> char = <fun>
# capitalize 'h';;
- : char = 'H'
# capitalize 'H';;
- : char = 'H'
# capitalize '1';;
- : char = '1'
# #quit;;

休憩してから何かやりましょうか。