落書きノート

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

OCaml 練習

Rustは後回しにしてOCamlで基礎を少し…。

        OCaml version 4.03.0

# 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
# val area_circle2 : float = 12.566370614
  ;;
Error: Syntax error
# 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
# let a_2' = 3.14;;
val a_2' : float = 3.14
# a_2'
  ;;
- : float = 3.14
# let ___ = 3.14;;
val ___ : float = 3.14
# ___;;
- : float = 3.14
# let Cat = 3.14;;
Error: Unbound constructor Cat
# let _'_'_ = 3.14;;
val _'_'_ : float = 3.14
# _'_'_;;
- : float = 3.14
# let 7eleven = 3.14;;
Error: Invalid literal 7eleven
# let 'ab2_ = 3.14;;
Error: Syntax error
# let _ = 3.14;;
- : float = 3.14
# _
  ;;
Error: Syntax error: wildcard "_" not expected.
# let area_of_circle r = (* 半径 r の円の面積 *)
  r *. r *. pi;;
val area_of_circle : float -> float = <fun>
# area_of_circle 2.0;;
- : float = 12.566370614
# let area_of_circle : float) : float = (* 半径 r (実数)の円の面積 *) r *. r *. pi;;;;
Error: Syntax error
# let area_of_circle(r:float):float= r *. r *. pi;;
val area_of_circle : float -> float = <fun>
# let abs x = if x > 0 then x else -x;;
val abs : int -> int = <fun>
# abs 5;;
- : int = 5
# abs (-3);;
- : int = 3
# 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
# 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
# #quit;;