FIL3000 ForSyDe Assignment 2 Ingo Sander March 25, 2011 1. Preparations (a) If not already installed, install the version control system darcs on your computer. You can use cabal install darcs, if cabal-install is installed. (b) Create a darcs repository assignment-yourname for the assignments of the course. Create a directory assignment-01 and copy the file Assignment1.hs into this directory. (c) Create a directory assignment-02, where you store all files for this assignment. 2. Store the abstract data type for a ’function tree’ FTree with all functions from assignment 1 in a separate module FTree and save it as FTree.hs. (a) Instead of using the function showTree make FTree an instance of the type class Show. *FTree> let myTree = Branch Mul (Branch Add (Value 1) (Value 2)) (Value 5) *FTree> myTree "(Mul (Add 1 2) 5)" (b) Make FTree an instance of the type class Eq and of the type class Ord. Here the order is defined by the computed result of the FTree. 1 (c) Create a function balanceTree :: FTree a -> FTree a that balances an FTree. A balanced tree is a tree with the property that the depth of the left and right subtrees in each node differ by at most one. If this is not possibly the difference in depth should not be more than one. Since a wrong balancing of a tree can give a different result, only transformations shall be allowed that do not affect the computation result of the tree. NOTE: It might be impossible to achieve a balanced tree for each FTree. i. Develop a function balanceTree1 using the assumption that all operations are equal (either only additions or only multiplications). ii. Try to further develop the function balanceTree1 into balanceTree, which tries to balance FTrees, where different operations are used in the subnodes (see above)1 . 3. Create a cabal package RTOS, which is a library. It includes the module RTOS that defines a data type Task for a periodic task as follows2 : data Task = T4 Phase Period Execution Deadline | T2 Period Execution deriving (Show, Eq) where Phase, Period, Execution, and Deadline shall be identical to the type TimeUnit, which is again is identical to the type Int. The data constructor T4 shall be used for the representation of the task properties as four-tuple (Φ, p, e, d)3 , while the data constructor T2 shall be used, if the task is represented as a tuple (p, e). In this case the Φ equals to 0, and d = p. Another data type specifies the scheduling method: data Method = RM -- rate-monotonic algorithm | DM -- deadline-monotonic algorithm | EDF -- earliest-deadline-first deriving (Show,Eq) 1 This subtask is quite tricky, but I expect that you should give it a decent try. If necessary, you may later extend the definition of the data type Task. 3 Φ denotes the phase, p the period, e the execution time, and d the relative deadline of the task. 2 2 (a) Define and export the function periodicSchedule :: Method -> [Task] -> [(TimeUnit, Maybe Task)] that shows the periodic schedule for a given scheduling method (RM, DM, EDF) for one hyperperiod. prompt> cabal install Resolving dependencies... Configuring RTOS-0.1... Preprocessing library RTOS-0.1... Building RTOS-0.1... [1 of 1] Compiling RTOS ( RTOS.hs, dist/build/RTOS.o ) Registering RTOS-0.1... Installing library in /home/ingo/.cabal/lib/RTOS-0.1/ghc-6.12.3 Registering RTOS-0.1... prompt> ghci GHCi, version 6.12.3: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer-gmp ... linking ... done. Loading package base ... linking ... done. Loading package ffi-1.0 ... linking ... done. Prelude> :m +RTOS Prelude RTOS> let taskSet = [T2 3 1,T2 5 1,T2 10 2] Loading package RTOS-0.1 ... linking ... done. Prelude RTOS> periodicSchedule RM taskSet [(0,Just (T4 0 3 1 3)),(1,Just (T4 0 5 1 5)), (2,Just (T4 0 10 2 10)),(3,Just (T4 0 3 1 3)), (4,Just (T4 0 10 2 10)),(5,Just (T4 0 5 1 5)), (6,Just (T4 0 3 1 3)),(7,Nothing),(8,Nothing), (9,Just (T4 0 3 1 3)),(10,Just (T4 0 5 1 5)), (11,Just (T4 0 10 2 10)),(12,Just (T4 0 3 1 3)), (13,Just (T4 0 10 2 10)),(14,Nothing), (15,Just (T4 0 3 1 3)),(16,Just (T4 0 5 1 5)), (17,Nothing),(18,Just (T4 0 3 1 3)),(19,Nothing), (20,Just (T4 0 5 1 5)),(21,Just (T4 0 3 1 3)), (22,Just (T4 0 10 2 10)),(23,Just (T4 0 10 2 10)), (24,Just (T4 0 3 1 3)),(25,Just (T4 0 5 1 5)), (26,Nothing),(27,Just (T4 0 3 1 3)), (28,Nothing),(29,Nothing)] 3 (b) Write a function periodicScheduleNames :: Method -> [Task] -> [(TimeUnit, String)] that output the name of the task instead of the task properties. The first task in the list has the name ”T1”, the second task the number ”T2”, and so on. (c) Write a function showSchedule :: [(TimeUnit, Maybe Task)] -> String that shows the schedule in a clearer way than above. 4. Create a module Stack, which exports the data type Stack a and the following functions: -- ’isEmpty’ checks, if the stack is empty isEmpty :: Stack a -> Bool -- ’push’ pushes a new element from the stack push :: Stack a -> a -> Stack a -- ’pop’ removes an element from the stack pop :: Stack a -> (Stack a, a) -- ’stack’ creates a stack from a list. The first element -- in the list will be the top of the stack. stack :: [a] -> Stack a NOTE: The data constructors shall not be exported! 5. Create a module RPNCalculator that implements a calculator that uses reverse polish notation. The calculator shall use the module Stack from task 4 to perform the calculation. In order to calculate for instance the term (4 + 3) * 5 - 6 you can write *RPNCalculator> let s = [Val 4, Val 3, Add, Val 5, Mul, Val 6, Sub] *RPNCalculator> calc s 29 where you have to create an own data type for the RPN-language. In case of a non-well-formed stack an error message shall occur. 4 *RPNCalculator> let s = [Val 4, Val 5, Add, Mul] *RPNCalculator> calc s *** Exception: Cannot pop from empty stack 6. Implement the game Mastermind in Haskell using the I/O-Monad. Create a cabal package Mastermind that installs the game as executable. prompt> cabal install Resolving dependencies... Configuring Mastermind-0.1... Preprocessing executables for Mastermind-0.1... Building Mastermind-0.1... [1 of 1] Compiling Main ( Main.hs, dist/build/ Mastermind/Mastermind-tmp/Main.o ) Linking dist/build/Mastermind/Mastermind ... Installing executable(s) in /home/ingo/.cabal/bin prompt> Main Welcome to the game Mastermind! You have to guess a combination of four items using six colors (red (R), green (G), blue (B), yellow (Y), orange (O) and violet (V). Each time you will get information about your guess: 1) If it 2) If it 3) If be an existing color is at the correct place, will be indicated by a black (B) marker. an existing color is at the wrong place, will be indicated by a white (W) marker. there is a non-existing color, it will indicated by no marker. Have fun! Make your guess: BRC Invalid guess 5 Make your guess: OGYR "WW" Make your guess: BROV "WWWW" Make your guess: ROVB "BBWW" Make your guess: RBVO Congratulations! You solved the puzzle in 3 turns. (a) The four-color combination shall be read from a file. (b) The four-color combination shall be randomly generated. 7. Polishing (a) Use the tool hlint to get suggestions how to improve your source code. (b) Generate html-documentation for your modules using the tool haddock with the switch -h. (c) Do not forget to store your source files for the assignment in a directory assignment-02 into a darcs-repository and make it available to the course leader. Do not forget to provide the course leader with the path to the repository and read access rights! This means that the repository needs to be located on the AFS-file system! NOTE: New tools like hlint and haddock can be installed using cabal install toolname, if you have installed the packages cabal and cabal-install on your machine. DEADLINE: The deadline for the assignment is specified on the course web page. 6
© Copyright 2026 Paperzz