Functional programming

From Marek Běhálek Wiki
Revision as of 13:20, 1 November 2019 by FuzzyBot (talk | contribs) (Updating to match new version of source page)
Jump to navigation Jump to search

Basic introduction

The course introduces the functional style of programming. It covers basic properties of the functional programming like: the side effect-free programming, functions as first-class values, high-order functions, recursion, pattern matching, or function closures. Also, course introduces selected data structures like a list and a tree and a functional style of working with these structures. As a programming language, Haskell will be used. It is a pure functional, statically typed, lazy evaluated language.

Subject aims expressed by acquired skills and competences

The basic outcome of this course will be the ability to write simple algorithms using a functional style of programming. More precisely, students will understand recursion and recursive data structures, they will be able to use high-order functions, and they will be able to define functions using the pattern matching. They will be able to use functional encapsulation mechanisms such as closures and modular interfaces and correctly reason about variables and lexical scope in programs. On practical level, they will be able to write these basic algorithms in programming language Haskell. Moreover, they will be able to recognize functional style of programming, they will understand advantages and disadvantages of this style of programming and they will be able to compare this style of programming with other approaches like imperative or object-oriented programming.

Recommended literature

  • Lipovaca M.:Learn You a Haskell for Great Good!: A Beginner's Guide (1st ed.). No Starch Press, San Francisco, CA, USA, 2011 - for free at: http://learnyouahaskell.com/
  • Thompson S.: The Haskell: The Craft of Functional Programming (3nd ed.). Addison-Wesley Professional, October 2, 2011, ISBN-10: 0201882957.

Way of continuous check of knowledge in the course of semester

During the exercises, students will be programming assigned tasks. The results of these tasks will be the crucial part of the final evaluation. Additionally, two smaller projects will be given.

Software

In our lectures, we will be using Haskell Platform with Visual Studio Code. For VS Code, we will be using two extensions:

Installation guide Windows (single user)

  1. Download and install Haskell platform from https://www.haskell.org/platform/windows.html
    • Use default paths. Installer adds to system variable PATH a path to executable stack.exe.
  2. Download and install VS Code https://code.visualstudio.com/#alt-downloads
  3. Open cmd and type:
    stack install intero phoityne-vscode haskell-dap --system-ghc
  4. Open file c:\sr\config.yaml and add a line
    system-ghc : true
    • Attribute --system-ghc is optional and redundant to previous step (it is the same setting, but global).
  5. Open VS Code and install extensions:
    • Haskero
    • Haskell GHCi Debug Adapter Phoityne

Installation guide Windows (all users)

This guide assumes, that there is on user, that prepares the development environment for other user.

  1. Download and install Haskell platform from https://www.haskell.org/platform/windows.html
  2. * For stack, use a path accessible to all user (for example c:\stack). Installer adds to user variable PATH a path to executable stack.exe, and adds a new user variable STACK_ROOT. Move these values from user variables to system variables.
  3. Download and install VS Code https://code.visualstudio.com/#alt-downloads. Use the system installer.
  4. Open cmd and type:
    stack install intero phoityne-vscode haskell-dap --system-ghc
    • It will generate some executable files to default location: c:\Users\ -- YOUR NAME -- \AppData\Roaming\local\bin\ move them to the directory with stack.exe.
  5. Open file c:\sr\config.yaml and add lines:
    system-ghc : true
    skip-msys : true
    

    These settings save time and space on disk (it will not install GHC while installing packages and MSYS2 for every user).

  6. Open VS Code and install extensions:
    • Haskero
    • Haskell GHCi Debug Adapter Phoityne

Installation guide Ubuntu (single users)

  1. Perform following commands:
    sudo apt-get install haskell-platform
    sudo apt-get install haskell-stack
    sudo snap install code --classic
    
    • Optional step - Right now (August, 2019) Haskell Platform from Ubuntu repositories contains GHC 8.4.4. Stack current default resolver is lts-14.00. It requires GHC version 8.6.5. It will automatically download this version. To save disk space, you can generate stack global config (for example stack path) and change the global resolver in file ~/.stack/global-project/stack.yaml to lts-12.15. Then open file ~/.stack/config.yaml and add lines:
    system-ghc : true
    skip-ghc-check: true
    
  2. Continue with installation of required packages:
    stack install intero phoityne-vscode haskell-dap
    
  3. Open VS Code and install extensions:
    • Haskero
    • Haskell GHCi Debug Adapter Phoityne

Installation guide Ubuntu (all users)

  1. Perform following commands:
    sudo apt-get install haskell-platform
    sudo apt-get install haskell-stack
    sudo snap install code --classic
    
  2. Create a directory that will be a stack root, it should be readable by all users (for example /opt/stack).
  3. Create a file /etc/profile.d/stack.sh with following line:export STACK_ROOT=your stack root directory
  4. Create a file /etc/stack/config.yaml with following lines:
    skip-ghc-check: true
    system-ghc: true
    allow-different-user: true
    
  5. Generate stack global config (for example stack path) and change the global resolver in file $STACK_ROOT/global-project/stack.yaml to
    • lts-12.15 for Ubuntu 19.04 (ghc 8.4.4)
    • lts-8.0 for Ubuntu 18.04 (ghc 8.0.2)
  6. Continue with installation of required packages:
    stack install intero phoityne-vscode haskell-dap
    
  7. It will produce some executable files to ~/.local/bin copy them to: /usr/local/bin (or other search when executing location).
  8. Open VS Code and install extensions:
    • Haskero
    • Haskell GHCi Debug Adapter Phoityne
    code --install-extension phoityne.phoityne-vscode 
    code --install-extension Vans.haskero

Presentations

Presentations used in our lectures:

Introduction to FPR

Basics of functional Programming

Schedule:

Laboratories

Solutions to tasks form laboratories: Solution

Cheat sheet containing basic functions in PDF

Homework

Combine study