wtd
|
Posted: Wed Mar 16, 2005 5:03 pm Post subject: Syntax Quickref: Turing <--> Eiffel |
|
|
Eiffel for the Turing Programmer
Input-Output
Printing to the standard output.
Turing
Eiffel
code: | std_output.put_string("Hello")
std_output.put_new_line |
The same, without skipping to a newline.
Turing
Eiffel
code: | std_output.put_string("Hello") |
Printing an integer.
Turing
Eiffel
code: | std_output.put_integer(42)
std_output.put_new_line |
Printing a floating point number.
Turing
Eiffel
code: | std_output.put_real(42.3)
std_output.put_new_line |
Getting a line from the keyboard.
Turing
code: | var line : string
get line : * |
Eiffel
code: | test_proc is
local
line : STRING
do
std_input.read_line
line := std_input.last_string
end |
Getting a single character from the keyboard.
Turing
code: | var ch : string
get ch : 1 |
Eiffel
code: | test_proc is
local
ch : CHARACTER
do
std_input.read_character
ch := std_input.last_character
end |
Getting an integer from the keyboard.
Turing
Eiffel
code: | test_proc is
local
i : INTEGER
do
std_input.read_integer
i := std_input.last_integer
end |
Getting a float from the keyboard.
Turing
Eiffel
code: | test_proc is
local
f : REAL
do
std_input.read_real
f := std_input.last_real
end |
Math
code: |
Turing | Eiffel | Result
----------+--------------+---------
4 + 3 | 4 + 3 | 7
4 - 2 | 4 - 2 | 2
2 * 3 | 2 * 3 | 6
2 / 5 | 2 / 5 | 0.4
6 div 4 | | 1
6 rem 4 | 6 \\ 4 | 2
3 ** 2 | 3 ^ 2 | 9.0
sin | sine |
cos | cosine |
| tangent |
| arc_sine |
| arc_cosine |
| arc_tangent | |
Comparisons
code: |
Turing | Eiffel
--------+----------
= | =
not= | /=
<, <= | <, <=
>, >= | >, >=
not | not
and | and/and then
or | or/or else |
Conditional Statements/Expressions
code: | Turing | Eiffel
----------------------------+------------------------------
if condition then | if condition then
statement1 | statement1
elsif otherCondition then | elsif otherCondition then
statement2 | statement2
else | else
defaultStatement | defaultStatement
end if | end
----------------------------+------------------------------
case value of | inspect value
label 1: statement1 | when 1 then statement1
label 2: statement2 | when 2 then statement2
label: default | else default
end case | end |
Function Definition
code: | Turing | Eiffel
------------------------------------+--------------------------------------------
function fooBar : string | foo_bar : STRING is
result "foo, bar" | do
end fooBar | Result := "foo, bar"
| end
------------------------------------+--------------------------------------------
function square (n : int) : float | square(n : INTEGER) : REAL is
result n ** 2 | do
end square | Result := n ^ 2
| end
------------------------------------+--------------------------------------------
procedure greet (name : string) | greet(name : STRING) is
put "Hello, " + name | do
end greet | std_input.put_string("Hello, " + name)
| std_input.put_new_line
| end
------------------------------------+--------------------------------------------
procedure greet (name : string) | greet(name : STRING) is
var greeting := "Hello, " + name | local
put greeting | greeting : STRING
end greet | do
| greeting := "Hello, " + name
| std_input.put_string("Hello, " + name)
| std_input.put_new_line
| end
------------------------------------+--------------------------------------------
function fact (n : int) : int | fact(n : INTEGER) : INTEGER is
if n = 0 then | do
result 1 | if n = 0 then
else | Result := 1
result n * fact (n - 1) | else
end if | Result := n * fact(n - 1)
end fact | end
| end |
Comments
code: | Turing | Eiffel
------------+-------------
% | --
/* ... */ | |
More to come. |
|
|