Difference between revisions of "FP Test3 2025"

From Marek Běhálek Wiki
Jump to navigation Jump to search
Line 104: Line 104:
  
 
<translate>
 
<translate>
Using the same coordinates as for the functions above, define a function <code>getPart</code> that takes a maze and extracts a rectangular part of this maze. The maze is given as first parameter. The extracted part is defined by the position of the top left corner (second parameter) and its size (third parameter, given as (height, width)).
+
Implement a function <code>processOrders</code> that processes a list of orders (drink names) in sequence.
 +
It returns:
 +
  ** <code>Just</code> a new machine with the updated stock if all drinks can be served, or
 +
  ** <code>Nothing</code> if any of the orders cannot be fulfilled.
 
</translate>
 
</translate>
  

Revision as of 07:26, 11 November 2025

Home Preparation For Test 3 - 2025

(Theme: Vending Machine)

Data types and examples

Let's have a data types representing the state of a vending machine:

  • a type Stock for the available ingredients (name and quantity),
  • a type Drink with the drink’s name, price, and list of required ingredients,
  • a type Machine that combines both stock and available drinks.
type Stock = [(String, Int)]
data Drink = Drink { name :: String , price :: Int , ingredients :: [String] } deriving Show
data Machine = Machine {stock :: Stock, drinks :: [Drink] } deriving Show

stockExample :: Stock
stockExample = [ ("water", 10) , ("coffee", 4) , ("milk", 2) , ("tea", 3) , ("sugar", 5) , ("cocoa", 1) ]

drinksList :: [Drink]
drinksList = [ 
    Drink "Coffee" 30 ["water","coffee"],
    Drink "StrongCoffee" 40 ["water","coffee","coffee"],
    Drink "Tea" 25 ["water", "water","tea"],
    Drink "Cappuccino" 45 ["water","coffee","milk"],
    Drink "Latte" 50 ["water","coffee","milk","sugar"],
    Drink "Chocolate" 50 ["water","cocoa","milk"],
    Drink "Cocoa" 60 ["milk","cocoa","milk"]
    ]

machineExample :: Machine
machineExample = Machine stockExample drinksList

Tasks

Create functions that:

  • Implement a function findDrink that finds a drink by name from a list.

If the drink does not exist, raise an error "No such drink".

findDrink :: [Drink] -> String -> Drink
ghci> findDrink drinksList "Tea"  
Drink {name = "Tea", price = 25, ingredients = ["water","water","tea"]}
  • Implement a function inStock that checks whether a given ingredient is currently available in stock.
inStock :: Stock -> String -> Bool
ghci> inStock stockExample "milk"
True
ghci> inStock stockExample "juice"
False
  • Implement a function useIngredient that decreases the quantity of a given ingredient by one unit.

Ingredients with zero quantity should be removed from the stock.

useIngredient :: Stock -> String -> Stock
ghci> useIngredient  stockExample "milk"
[("water",10),("coffee",4),("milk",1),("tea",3),("sugar",5),("cocoa",1)]
ghci> useIngredient  stockExample "cocoa"
[("water",10),("coffee",4),("milk",2),("tea",3),("sugar",5)]
  • Implement a function canServeDrink that checks whether all ingredients required for a drink are available.
canServeDrink :: Stock -> Drink -> Bool
ghci> canServeDrink stockExample (findDrink drinksList "Cocoa")
True
  • Implement a function serveDrink that updates the stock after preparing a drink.

If some ingredient is missing, raise an error "Can not serve this drink".

serveDrink :: Stock -> Drink -> Stock
ghci> serveDrink stockExample (findDrink drinksList "Cocoa")   
[("water",10),("coffee",4),("tea",3),("sugar",5)]
  • Implement a function processOrders that processes a list of orders (drink names) in sequence.

It returns:

 ** Just a new machine with the updated stock if all drinks can be served, or
 ** Nothing if any of the orders cannot be fulfilled.
getPart :: Maze -> (Int,Int) -> (Int,Int) -> Maze
ghci> printMaze(getPart sample1 (1,1) (7,7))
 *   *
 * * *
 * * *
   *
******

*******
  • Implement the function solveMaze. It has 1 argument. It is a list of strings representing a maze row by row from top to bottom ('*' - wall, ' ' - empty square, 's' - starting position, 'e' - ending possition). At the beginning we are at position 's' and we want to get the length of the shortest path to the position denotated by 'e'. Such path compose from steps. in each step we can move one square left, right, up or down. The function returns a number of these steps in the shortest path from 's' to 'e'.
solveMaze :: Result -> Int
solveMaze sample4 
12