Difference between revisions of "PLC Laboratory 1"

From Marek Běhálek Wiki
Jump to navigation Jump to search
 
(11 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Working environment ==
+
== Interpreter of Arithmetic Expressions ==
  
Using the installation guide from [[Functional programming | Functional programming]] prepare your working environment.
+
Implement an interpreter of arithmetic expressions. These expressions contain <code>+, -, *, /</code> operators (with common priorities and left associativity) and parentheses.  
  
===How to verify it is working?===
+
To simplify the task, consider we have only binary operators. There are no unary operators in our language. Moreover, we can use only positive integers in our expressions.
  
*Run <code>ghci</code> from the command line. it will start GHC Interpreter, the output should be something like this:
+
== Input specification ==
<syntaxhighlight lang="Haskell" class="myDark" >
+
 
GHCi, version 8.6.5: http://www.haskell.org/ghc/  :? for help
+
The first line of the input contains a number <code>N</code>. It defines the number of expressions your program should evaluate. These expressions are on next <code>N</code> lines. Each line contains exactly one expression.  
Prelude>
+
 
</syntaxhighlight >
+
== Output specification ==
*If you write expression, for example <code>1+2*3</code>,  it should be evaluated. You can close this interpreter by typing <code>:q</code>.
+
 
*Open Visual Studio Code, create a file <code>simple.hs</code> and put there following lines of code:
+
For each expression write one line containing the result – the computed value of the expression. If there is any error in the input, write text <code>ERROR</code> instead.
<syntaxhighlight lang="Haskell" >
 
doubleMe x = x * x
 
  
plus x y  = x + y + 'a'
+
== Example ==
 +
* Input
 +
<syntaxhighlight lang="haskell" >
 +
2
 +
2 * (3+5)
 +
15 - 2**7
 
</syntaxhighlight >
 
</syntaxhighlight >
It should report a problem in function <code>plus</code> (you can not use <code>+</code> with letter, it can be repaired by removing <code>+ 'a'</code>). Report from VS Code is refreshed whenever the source file is saved on disk.
 
 
== First program in Haskell ==
 
In your favorite development environment (VS Code by default):
 
* Crate a file <code>simple.hs</code>.
 
* Create a function <code>pythagoras a b</code> that computes <code>c</code> based on  <math>c^2 = a^2 + b^2 \,,</math>. Necessary functions can be found: [https://hoogle.haskell.org/ Hoogle]
 
* Open <code>ghci</code> and run the implemented function with <code>3 4</code>. File can be loaded using command <code>:l</code> (<code>:load</code>) and reloaded with <code>:r</code> (<code>:reload</code>).
 
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 
<syntaxhighlight lang="Haskell">
 
pythagoras a b = sqrt (a*a + b*b)
 
</syntaxhighlight>
 
[[File:Tryit.png|center|60px|Try it!|link=https://rextester.com/DEAQ69276]]
 
</div>
 
<div style="clear:both"></div>
 
== Real first program in Haskell ==
 
  
If you are complaining, that all programming courses should start with printing <code>"Hello world!"</code> on the screen and that is why previous task sucks. Do the following exercise.
+
* Output
*Crate a file <code>firstProgram.hs</code> with following content:
+
<syntaxhighlight lang="haskell" >
<syntaxhighlight lang="Haskell">
+
16
main = do putStr "Hello world!"
+
ERROR
 
</syntaxhighlight >
 
</syntaxhighlight >
*Compile it with 'GHC compiler' (command <code>ghc</code>).
 
*It should produce an executable file <code>firstProgram(.exe)</code>, run this file.
 

Latest revision as of 08:24, 26 January 2022

Interpreter of Arithmetic Expressions

Implement an interpreter of arithmetic expressions. These expressions contain +, -, *, / operators (with common priorities and left associativity) and parentheses.

To simplify the task, consider we have only binary operators. There are no unary operators in our language. Moreover, we can use only positive integers in our expressions.

Input specification

The first line of the input contains a number N. It defines the number of expressions your program should evaluate. These expressions are on next N lines. Each line contains exactly one expression.

Output specification

For each expression write one line containing the result – the computed value of the expression. If there is any error in the input, write text ERROR instead.

Example

  • Input
2
2 * (3+5)
15 - 2**7
  • Output
16
ERROR