Links und Funktionen
Sprachumschaltung

Navigationspfad
Sie sind hier: Startseite / Lehre / SS 2012 / Funktionale Programmierung / Übungen / Übung 02


Inhaltsbereich

Übung 02

Blatt 2: Listen, I/O, Monaden

Haskell source code icon 02Sheet.hs — Haskell source code, 3 KB (3114 bytes)

Dateiinhalt

-- | Functional Programming, course at LMU, summer term 2012
--   Andreas Abel and Steffen Jost
--
-- Exercise sheet 2, 2012-05-03
--
-- Instructions:
--
-- Replace all occurrences of 'undefined' and 'Undefined' by sensible code.
-- Do not change the type of functions you are asked to implement!
--
-- Submit your solutions via UniWorX.

----------------------------------------------------------------------
-- Header
--
-- You can add imports and LANGUAGE pragmas here.
----------------------------------------------------------------------

module Main where

import Control.Applicative ((<$>))
import Control.Monad

import qualified Data.List as List

import Debug.Trace

import System.Directory
import System.Environment
import System.FilePath

----------------------------------------------------------------------
-- Exercise 1  Sudoku-Board as list of lists
----------------------------------------------------------------------

-- | A completely filled Sudoku board is represented as a list of @n*n@ rows
--   which are each a list of @n*n@ numbers between 1 and @n*n@ (inclusively).
type Sudoku = [Row]
type Row    = [Int]

-- | A (completely filled) Sudoku board is valid if all its @rows@, @columns@
--   ans @n×n@-@blocks@ are permutations of @1..n*n@.
validSudoku :: Int -> Sudoku -> Bool
validSudoku n s = undefined

----------------------------------------------------------------------
-- Exercise 2 Printing the directory tree
----------------------------------------------------------------------

-- | Print the directory tree recursively, starting with directory @root@.
--   Hidden files are directories are skipped.
--
--   For instance, a directory structure
--
--     @
--     root
--       aDir
--         aFile1
--       bDir
--         cDir
--           cFile1
--       dFile
--
--     @
--
--  is printed as
--
--    @
--       aDir
--       bDir
--         cDir
--    @
--
--  with sensible indentation to express child and sibling relations.
mainDir :: FilePath -> IO ()
mainDir root = undefined

main :: IO ()
main = do
  args <- getArgs
  mainDir $ if null args then "." else head args

----------------------------------------------------------------------
-- Exercise 3  Lazy monadic Boolean operators
----------------------------------------------------------------------

-- Conjunction and Disjunction are lazy

verbose :: Bool -> Bool
verbose b = trace ("encountered " ++ show b) b

tA = [True, False, True, False]
tO = [False, True, False, True]

testA = and $ map verbose tA
testO = or  $ map verbose tO

-- Naive adaption to a monad makes them strict

verboseM :: Bool -> IO Bool
verboseM b = putStrLn ("encountered " ++ show b) >> return b

testMA = and <$> mapM verboseM tA
testMO = or  <$> mapM verboseM tO

-- Implement lazy monadic conjunction and disjunction!

andM :: Monad m => [m Bool] -> m Bool
andM = undefined

orM  :: Monad m => [m Bool] -> m Bool
orM  = undefined

-- The following test cases should only print those Booleans
-- that are necessary to determine the result of the operation!

testLA = andM $ map verboseM tA
testLO = orM  $ map verboseM tO

Artikelaktionen


Funktionsleiste