FP / Návrh funkcionálnych programov

Návrh funkcionálnych programov

S prípadovou štúdiou

Moduly

Modul

module BTree ( BTree(Leaf,Node), mapBTree ) where

data BTree = ...

mapBTree ...

Export konštruktorov

module BTree (BTree, mapBTree) where
module BTree (BTree(..), mapBTree) where

Import

import Tree
import BTree ( BTree(Leaf,Node), mapBTree )

Kvalifikovaný import

import qualified BTree ( BTree(Leaf,Node), mapBTree )

myTree = BTree.Node (BTree.Leaf 1) (BTree.Leaf 2)
secondTree = BTree.mapBTree (+1) myTree

Skrátenie názvu

import qualified Data.Map as M

Prípadová štúdia: 🚩 míny

Princípy

Dekompozícia

Typy dát

data Board = ...
data Config = Config Int Int Int
data Tile = Open Int | Closed | Marked | Exploded
data Pos = Pos Int Int
data Move = MarkTile Pos | OpenTile Pos
data MoveResult = Playing | Solved | Failed

Hlavné funkcie

tileState :: Board -> Pos -> Tile
gameMove :: Board -> Move -> (MoveResult, Board)
play :: Config -> Solver -> IO Bool

Solver?

Solver

type Solver = Board -> IO Move

Ako vytvoriť hraciu dosku?

createBoard :: Config -> Board

Potrebujeme náhodu

createBoard :: RandomGen g => g -> Config -> Board

Konfigurácia projektu

Cabal

Show me
the code!