
-----------------------------------
wtd
Thu Mar 03, 2005 7:42 pm

Syntax Quickref: Turing &lt;--&gt; Haskell
-----------------------------------
Haskell for the Turing Programmer

Input-Output

Printing to the standard output.

Turing
put "Hello"

Haskell 
putStrLn "Hello"

The same, without skipping to a newline.

Turing
put "Hello" ..

Haskell
do putStr "Hello"
   hFlush stdout
   
Printing an integer.

Turing
put 42

Haskell
putStrLn (show 42)

Printing a floating point number.

Turing
put 42.3

Haskell
putStrLn (show 42.3)

Getting a line from the keyboard.

Turing
var line : string
get line : *

Haskell
line :: IO String
line = getLine

Getting a single character from the keyboard.

Turing
var ch : string
get ch : 1

Haskell
ch :: IO Char
ch = getChar

Getting an integer from the keyboard.

Turing
var i : int
get i

Haskell
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  Float
readFloat str = case reads str of
  []      -> 0
  [(n,_)] -> n
  _       -> error "I said a float, you insolent hacker!"
  
f :: IO Float  
f = do l  expression1
   label 2: statement2      |    2 -> expression2
   label: default           |    _ -> default
 end case                   |

Function Definition

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

Turing      | Haskell     
------------+-------------
 %          |  --      
 /* ... */  |  {- ... -}   

More to come.
