Re: [Haskell-cafe] The simple programming language,

2006-03-26 Thread Cale Gibbard
Well, I'm sure that lots of people here would be willing to help if
you'd be more specific about what you're having trouble with. Just
posting an assignment problem tends not to generate much discussion
until after the assignment deadline. What have you already tried? What
part are you stuck on?

Also, if you're interested in more immediate responses, try #haskell
on irc.freenode.net.

For help with asking good questions, check out
http://www.haskell.org/hawiki/HomeworkHelp

 - Cale

P.S.:
(Just to let everyone know, this is an assignment problem from the CSA
1080 Declarative Programming course at the University of Malta. The
relevant URL is here: http://www.cs.um.edu.mt/~gpac1/ I've gotten a
few questions about it lately, and there was a post to the libraries
list about another problem on it.)


On 26/03/06, pinturicchio <[EMAIL PROTECTED]> wrote:
>
> 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
> expressed
> a datatype in Haskell.
> data Prg =
> Skip
> | 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
> expression
> to the output.
> Declare declares a variable, assigning its initial value to 0. The :=
> operator
> 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
> declarations.
> (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.
>
> ___
> Haskell-Cafe mailing list
> Haskell-Cafe@haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] The simple programming language,

2006-03-26 Thread pinturicchio

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
expressed
a datatype in Haskell.
data Prg =
Skip
| 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
expression
to the output.
Declare declares a variable, assigning its initial value to 0. The :=
operator
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
declarations.
(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.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe