Syntax Quickref: Turing <--> O'Caml
Author |
Message |
wtd
|
Posted: Thu Mar 03, 2005 9:13 pm Post subject: Syntax Quickref: Turing <--> O'Caml |
|
|
O'Caml for the Turing Programmer
Input-Output
Printing to the standard output.
Turing
O'Caml
code: | print_endline "Hello" |
The same, without skipping to a newline.
Turing
O'Caml
code: | print_string "Hello" |
Printing an integer.
Turing
O'Caml
Printing a floating point number.
Turing
O'Caml
Getting a line from the keyboard.
Turing
code: | var line : string
get line : * |
O'Caml
code: | let line = read_line () |
Getting a single character from the keyboard.
Turing
code: | var ch : string
get ch : 1 |
O'Caml
code: | let ch = let str = read_line () in
String.get str 0 |
Getting an integer from the keyboard.
Turing
O'Caml
code: | let i = read_int () |
Getting a float from the keyboard.
Turing
O'Caml
code: | let f = read_float () |
Math
code: |
Turing | O'Caml | Result
----------+-------------+---------
4 + 3 | 4 + 3 | 7
4 - 2 | 4 - 2 | 2
2 * 3 | 2 * 3 | 6
2 / 5 | 2 / 5 | 0
6 div 4 | 6 / 4 | 1
6 rem 4 | (mod) 6 4 | 2
| 6 mod 4 |
3 ** 2 | 3. ** 2. | 9.0
sin | sin |
cos | cos |
| tan |
| asin |
| acos |
| atan | |
Comparisons
code: | Turing | O'Caml
--------+----------
= | =
not= | !=
<, <= | <, <=
>, >= | >, >=
not | not
and | &&
or | ||, or |
Conditional Statements/Expressions
code: | Turing | O'Caml
----------------------------+------------------------------
if condition then | if condition
statement1 | then expression1
elsif otherCondition then | else if otherCondition
statement2 | then expression2
else | else defaultExpression
defaultStatement |
end if |
----------------------------+------------------------------
case value of | match value with
label 1: statement1 | | 1 -> expression1
label 2: statement2 | | 2 -> expression2
label: default | | _ -> default;;
end case | |
Function Definition
code: | Turing | O'Caml
------------------------------------+--------------------------------------------
function fooBar : string | let fooBar = "foo, bar"
result "foo, bar" |
end fooBar |
------------------------------------+--------------------------------------------
function square (n : int) : float | let square n = n ** 2
result n ** 2 |
end square |
|
------------------------------------+--------------------------------------------
procedure greet (name : string) | let greet name =
put "Hello, " + name | print_endline ("Hello, " ^ name)
end greet |
|
------------------------------------+--------------------------------------------
procedure greet (name : string) | let greet name =
var greeting := "Hello, " + name | let greeting = "Hello, " ^ name in
put greeting | print_endline greeting
end greet |
------------------------------------+--------------------------------------------
function fact (n : int) : int | let rec fact n =
if n = 0 then | if n = 0
result 1 | then 0
else | else n * fact (n - 1)
result n * fact (n - 1) |
end if |
end fact | |
Comments
code: | Turing | O'Caml
------------+-------------
% | none
/* ... */ | (* ... *) |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
|
|