Syntax Quickref: Turing <--> Haskell
Author |
Message |
wtd
|
Posted: Thu Mar 03, 2005 7:42 pm Post subject: Syntax Quickref: Turing <--> Haskell |
|
|
Haskell for the Turing Programmer
Input-Output
Printing to the standard output.
Turing
Haskell
The same, without skipping to a newline.
Turing
Haskell
code: | do putStr "Hello"
hFlush stdout |
Printing an integer.
Turing
Haskell
Printing a floating point number.
Turing
Haskell
code: | putStrLn (show 42.3) |
Getting a line from the keyboard.
Turing
code: | var line : string
get line : * |
Haskell
code: | line :: IO String
line = getLine |
Getting a single character from the keyboard.
Turing
code: | var ch : string
get ch : 1 |
Haskell
code: | ch :: IO Char
ch = getChar |
Getting an integer from the keyboard.
Turing
Haskell
code: | readInt :: String -> Int
readInt str = case reads str of
[] -> 0
[(n,_)] -> n
_ -> error "I said an int, you insolent hacker!"
f :: IO Int
f = do l <- getLine
return (readFloat l) |
Getting a float from the keyboard.
Turing
Haskell
code: | readFloat :: String -> Float
readFloat str = case reads str of
[] -> 0
[(n,_)] -> n
_ -> error "I said a float, you insolent hacker!"
f :: IO Float
f = do l <- getLine
return (readFloat l) |
Math
code: | Turing | Haskell | 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 | div 6 4 | 1
| 6 `div` 4 |
6 rem 4 | mod 6 4 | 2
| 6 `mod` 4 |
3 ** 2 | 3 ** 2 | 9.0
| 3 ^ 2 |
| 3 ^^ 2 |
sin | sin |
cos | cos |
| tan |
| asin |
| acos |
| atan | |
Comparisons
code: | Turing | Haskell
--------+----------
= | ==
not= | /=
<, <= | <, <=
>, >= | >, >=
not | not
and | &&
or | || |
Conditional Statements/Expressions
code: | Turing | Haskell
----------------------------+------------------------------
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 | case value of
label 1: statement1 | 1 -> expression1
label 2: statement2 | 2 -> expression2
label: default | _ -> default
end case | |
Function Definition
code: | Turing | Haskell
------------------------------------+--------------------------------------------
function fooBar : string | fooBar :: String
result "foo, bar" | fooBar = "foo, bar"
end fooBar |
------------------------------------+--------------------------------------------
function square (n : int) : float | square :: Int -> Float
result n ** 2 | square n = n ** 2
end square | or:
| square = (** 2)
------------------------------------+--------------------------------------------
procedure greet (name : string) | greet :: String -> IO ()
put "Hello, " + name | greet name = putStrLn ("Hello, " ++ name)
end greet | or:
| greet = putStrLn . ("Hello, " ++)
------------------------------------+--------------------------------------------
procedure greet (name : string) | greet :: String -> IO ()
var greeting := "Hello, " + name | greet name = let greeting = "Hello, " ++ name in
put greeting | putStrLn greeting
end greet | or:
| greet name = putStrLn greeting
| where
| greeting = "Hello, " ++ name
------------------------------------+--------------------------------------------
function fact (n : int) : int | fact :: Int -> Int
if n = 0 then | fact n = if n == 0
result 1 | then 0
else | else n * fact (n - 1)
result n * fact (n - 1) | or:
end if | fact 0 = 1
end fact | fact n = n * fact (n - 1) |
Comments
code: | Turing | Haskell
------------+-------------
% | --
/* ... */ | {- ... -} |
More to come. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
|
|