[Haskell-cafe] The simple programming language,

pinturicchio juventino_85 at hotmail.com
Sun Mar 26 09:17:41 EST 2006

if somebody can help me wid this i wil b realy thankful

Finally, we come to the program. The simple programming language,
which includes loops, conditionals and sequential composition will be
a datatype in Haskell.
data Prg =
| PrintS String
| PrintE Exp
| Declare Variable
| Variable := Exp
| Prg :> Prg
| IfThenElse Exp (Prg, Prg)
| While Exp Prg

The program Skip does nothing. The program PrintS ‘prints’ the given
string to the output. Similarly, PrintE prints the value of the given
to the output.
Declare declares a variable, assigning its initial value to 0. The :=
performs variable assignment.
The :> operator is sequential composition, while IfThenElse and While
are conditional and while-loops respectively. The condition in both cases
resolves to true if the value of the expression is not zero.
A factorial program can be programmed in this small language as follows:
counter = "counter"
result = "result"
factorial n =
Declare counter
:> Declare result
:> result := Val 1
:> counter := Val n
:> While (Var counter)
( result := (Var result) :*: (Var counter)
:> counter := (Var counter) :-: (Val 1)
:> PrintS ("Factorial of "++show n++" is:")
:> PrintE (Var result)
(a) The simulate function takes a Store and a program, and returns an
updated store and list of output strings:
simulate :: Store -> Prg -> (Store, [String])
Using pattern matching, define simulate for Skip, PrintS and variable
(b) Add the cases for PrintE and variable assignment.
(c) Sequential composition can be defined by simulating the two instructions
in sequence. Complete the following case:
simulate store (p1 :> p2) =
let (store’, outs1) = simulate store p1
(store’’,outs2) = simulate store’ p2
in ...
(d) Define simulate for conditionals.
(e) We can ‘cheat’ by simulating while-loops in terms of conditionals and
sequential compositi
simulate store (While condition program) =
simulate store
(IfThenElse condition
( program :> While condition program
, Skip
Using this definition, test your program with the factorial program.

View this message in context: http://www.nabble.com/The-simple-programming-language%2C-t1344638.html#a3596582
Sent from the Haskell - Haskell-Cafe forum at Nabble.com.

More information about the Haskell-Cafe mailing list