Difference between revisions of "FP Laboratory 1"

From Marek Běhálek Wiki
Jump to navigation Jump to search
 
(17 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
<translate>
 
<translate>
== Working environment ==
+
== Working environment == <!--T:1-->
  
 +
<!--T:2-->
 
Using the installation guide from [[Functional programming | Functional programming]] prepare your working environment.
 
Using the installation guide from [[Functional programming | Functional programming]] prepare your working environment.
  
===How to verify it is working?===
+
===How to verify it is working?=== <!--T:3-->
  
 +
<!--T:4-->
 
*Run <code>ghci</code> from the command line. it will start GHC Interpreter, the output should be something like this:
 
*Run <code>ghci</code> from the command line. it will start GHC Interpreter, the output should be something like this:
 
</translate>
 
</translate>
Line 13: Line 15:
 
</syntaxhighlight >
 
</syntaxhighlight >
 
<translate>
 
<translate>
 +
<!--T:5-->
 
*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>.
 
*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:
 
*Open Visual Studio Code, create a file <code>simple.hs</code> and put there following lines of code:
Line 22: Line 25:
 
</syntaxhighlight >
 
</syntaxhighlight >
 
<translate>
 
<translate>
 +
<!--T:6-->
 
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.
 
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 ==  
+
== First program in Haskell == <!--T:7-->
 
In your favorite development environment (VS Code by default):
 
In your favorite development environment (VS Code by default):
 
* Crate a file <code>simple.hs</code>.
 
* 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]
 
* 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>).
 
* 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>).
 +
</translate>
 +
<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>
 +
<translate>
 +
== Real first program in Haskell == <!--T:8-->
  
== Real first program in Haskell ==
+
<!--T:9-->
 
 
 
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.
 
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.
 
*Crate a file <code>firstProgram.hs</code> with following content:
 
*Crate a file <code>firstProgram.hs</code> with following content:
Line 39: Line 52:
 
</syntaxhighlight >
 
</syntaxhighlight >
 
<translate>
 
<translate>
 +
<!--T:10-->
 
*Compile it with 'GHC compiler' (command <code>ghc</code>).
 
*Compile it with 'GHC compiler' (command <code>ghc</code>).
 
*It should produce an executable file <code>firstProgram(.exe)</code>, run this file.
 
*It should produce an executable file <code>firstProgram(.exe)</code>, run this file.
 
</translate>
 
</translate>

Latest revision as of 14:59, 20 September 2021

Working environment

Using the installation guide from Functional programming prepare your working environment.

How to verify it is working?

  • Run ghci from the command line. it will start GHC Interpreter, the output should be something like this:
GHCi, version 8.6.5: http://www.haskell.org/ghc/  :? for help
Prelude>
  • If you write expression, for example 1+2*3, it should be evaluated. You can close this interpreter by typing :q.
  • Open Visual Studio Code, create a file simple.hs and put there following lines of code:
doubleMe x = x * x

plus x y  = x + y + 'a'

It should report a problem in function plus (you can not use + with letter, it can be repaired by removing + 'a'). 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 simple.hs.
  • Create a function pythagoras a b that computes c based on . Necessary functions can be found: Hoogle
  • Open ghci and run the implemented function with 3 4. File can be loaded using command :l (:load) and reloaded with :r (:reload).
pythagoras a b = sqrt (a*a + b*b)
Try it!

Real first program in Haskell

If you are complaining, that all programming courses should start with printing "Hello world!" on the screen and that is why previous task sucks. Do the following exercise.

  • Crate a file firstProgram.hs with following content:
main = do putStr "Hello world!"
  • Compile it with 'GHC compiler' (command ghc).
  • It should produce an executable file firstProgram(.exe), run this file.