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

個人的自由ノート

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

プログラミング in OCaml

文字型とlet束縛の文法確認。そして練習問題をやりました。セミコロン忘れる…。

        OCaml version 4.03.0

# 'a';;
- : char = 'a'
# '\120';;
- : char = 'x'
# int_of_char '\'';;
- : int = 39
# char_of_int 55;;
- : char = '7'
# char_of_int 800;;
Exception: Invalid_argument "char_of_int".
# "abc";;
- : string = "abc"
# "This is a pen.";;
- : string = "This is a pen."
# "Hello," ^ "World!";;
- : string = "Hello,World!"
# ("Hello," ^ " world!").[10
  ]
  ;;
- : char = 'l'
# ("Hello," ^ " world!").[10];;
- : char = 'l'
# - - 1
  ;;
- : int = 1
# -2 + 3
  ;;
- : int = 1
# 9 / -4;;
- : int = -2
# +3 + 5;;
- : int = 8
# float_of_int 3 +. 2.5;;
- : float = 5.5
# int_of_float 0.7;;
- : int = 0
# char_of_int ((int_of_char 'A') + 20)
  ;;
- : char = 'U'
# int_of_string "0xff"
  ;;
- : int = 255
# 5.0 ** 2.0;;
- : float = 25.
# 8 * -2;;
- : int = -16
# 8*-2
  ;;
Error: Unbound value *-
# int_of_string "0xfg";;
Exception: Failure "int_of_string".
# int_of_float -0.7;;
Error: This expression has type float -> int
       but an expression was expected of type int
# double_of_float -0.7;;
Error: Unbound value double_of_float
# float_of_int -0.7;;
Error: This expression has type int -> float
       but an expression was expected of type int
# int_of_int -0.7;;
Error: Unbound value int_of_int
# double_of_int -0.7;;
Error: Unbound value double_of_int
# -0.7;;
- : float = -0.7
# int_of_float 1.2;;
- : int = 1
# int_of_float -1.2;;
Error: This expression has type float -> int
       but an expression was expected of type int
# int_of_float (-1.2);;
- : int = -1
# 8*(-2);;
- : int = -16
# int_of_string "0xfg";;
Exception: Failure "int_of_string".
# int_of_string "\xfg";;
Warning 14: illegal backslash escape in string.
Exception: Failure "int_of_string".
# int_of_string ("0xfg");;
Exception: Failure "int_of_string".
# int_of_string "fg";;
Exception: Failure "int_of_string".
# int_of_string "12";;
- : int = 12
# int_of_string "0x";;
Exception: Failure "int_of_string".
# int_of_string "0xff";;
- : int = 255
# int_of_string "0xff";;
- : int = 255
# float_of_int int_of_float 5.0;;
Error: This function has type int -> float
       It is applied to too many arguments; maybe you forgot a `;'.
# float_of_int (int_of_float 5.0);; 
- : float = 5.
# sin 3.14 /. 2.0 ** 2.0 +. cos 3.14 /. 2.0 ** 2.0;;
- : float = -0.249601519702763175
# sin 3.14 /. (2.0 ** 2.0) + cos 3.14 /. (2.0 ** 2.0);;
Error: This expression has type float but an expression was expected of type
         int
# sin 3.14 /. (2.0 ** 2.0) +. cos 3.14 /. (2.0 ** 2.0);;
- : float = -0.249601519702763175
# int_of_float sin 3.14 /. 2.0 ** 2.0 +. cos 3.14 /. 2.0 ** 2.0;;
Error: This function has type float -> int
       It is applied to too many arguments; maybe you forgot a `;'.
# int_of_float (sin 3.14 /. 2.0 ** 2.0 +. cos 3.14 /. 2.0 ** 2.0);;
- : int = 0
# sin (3.14 /. 2.0 ** 2.0);;      
- : float = 0.706825181105366
# sin (3.14 /. 2.0);;
- : float = 0.99999968293183461
# cos (3.14 /. 2.0);;
- : float = 0.000796326710733263345
# cos (3.14 /. 2.0 ** 2.0);;
- : float = 0.707388269167199768
# sin (3.14 /. 2.0 ** 2.0) +. cos (3.14 /. 2.0 ** 2.0);;
- : float = 1.41421345027256562
# sin (3.14 /. 2.0 ** 2.0) +. cos 3.14 /. 2.0 ** 2.0;;
- : float = 0.456825498173481059
# int_of_float sin(3.14 /. 2.0 ** 2.0) +. cos(3.14 /. 2.0 ** 2.0);;
Error: This function has type float -> int
       It is applied to too many arguments; maybe you forgot a `;'.
# int_of_float (sin(3.14 /. 2.0 ** 2.0) +. cos(3.14 /. 2.0 ** 2.0));;
- : int = 1
# float_of_int int_of_float (sin(3.14 /. 2.0 ** 2.0) +. cos(3.14 /. 2.0 ** 2.0));;
Error: This function has type int -> float
       It is applied to too many arguments; maybe you forgot a `;'.
# float_of_int (int_of_float (sin(3.14 /. 2.0 ** 2.0) +. cos(3.14 /. 2.0 ** 2.0)));;
- : float = 1.
# sqrt 3 * 3 + 4 * 4;;
Error: This expression has type int but an expression was expected of type
         float
# sqrt (3 * 3 + 4 * 4);;
Error: This expression has type int but an expression was expected of type
         float
# sqrt(3) * 3 + 4 * 4;;
Error: This expression has type int but an expression was expected of type
         float
# sqrt 3.0 *. 3.0 +. 4.0 * 4.0;;
Error: This expression has type float but an expression was expected of type
         int
# sqrt 3.0 *. 3.0 +. 4.0 *. 4.0;;
- : float = 21.196152422706632
# sqrt (3.0 *. 3.0 +. 4.0 *. 4.0);;
- : float = 5.
# let pi = 3.1415926535;;
val pi : float = 3.1415926535
# pi;;
- : float = 3.1415926535
# 2.0 *. 2.0 *. pi;;
- : float = 12.566370614
# let area_circle2 = 2.0 *. 2.0 *. pi;; 
val area_circle2 : float = 12.566370614
# 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 : float = 3.1415926535
  let e = 2.718281828;;
val pi : float = 3.1415926535
val e : float = 2.718281828
# #quit;;