Declaratieve Talen/Oplossing Min-max: verschil tussen versies

Uit Wina Examenwiki
Naar navigatie springen Naar zoeken springen
WinaBot (overleg | bijdragen)
k Dtopl5 moved to Declaratieve Talen/Oplossing Min-max: correcte naamgeving
Beau (overleg | bijdragen)
Een oplossing:: alternatief
Regel 9: Regel 9:
  data Doosboom = Doos Int [Bolboom] Int deriving Show
  data Doosboom = Doos Int [Bolboom] Int deriving Show
  data Bolboom = Bol Int [Doosboom] Int deriving Show
  data Bolboom = Bol Int [Doosboom] Int deriving Show
 
  getWaardeBol :: Bolboom -> Int
  getWaardeBol :: Bolboom -> Int
  getWaardeBol (Bol id [] w) = w
  getWaardeBol (Bol id [] w) = w
Regel 31: Regel 31:
  berekenBolboom :: Bolboom -> Bolboom
  berekenBolboom :: Bolboom -> Bolboom
  berekenBolboom (Bol id bomen w) = Bol id [(berekenDoosboom d) | d <- bomen] (getWaardeBol (Bol id bomen w))
  berekenBolboom (Bol id bomen w) = Bol id [(berekenDoosboom d) | d <- bomen] (getWaardeBol (Bol id bomen w))
=== alternatieve oplossing ===
data Maxboom = Max Int [Minboom] Int deriving Show
data Minboom = Min Int [Maxboom] Int deriving Show
complete_max::Maxboom -> Maxboom
------------------------------
complete_max (Max id minbomen w) =
if w /= -1
then (Max id minbomen w)
else let completed_minbomen = map complete_min minbomen
max_w = maximum [winst | (Min id maxbomen winst) <- completed_minbomen]
    in (Max id completed_minbomen max_w)
complete_min::Minboom -> Minboom
--------------------------------
complete_min (Min id maxbomen (-1)) =
let completed_maxbomen = map complete_max maxbomen
    min_w = minimum [winst | (Max id minbomen winst) <- (map complete_max maxbomen)]
in (Min id completed_maxbomen (minimum [winst | (Max id minbomen winst) <- (map complete_max maxbomen)]))
complete_min (Min id maxbomen w) = (Min id maxbomen w)
--[[Gebruiker:Beau|Beau]] 17 jun 2006 17:34 (CEST)

Versie van 17 jun 2006 15:34

Een oplossing:

Probeer met volgend commando om bovenstaand voorbeeld te laten berekenen:

Main> berekenDoosboom (Doos 0 [Bol 1 [Doos 2 [] 1, Doos 3 [] 2] 2, Bol 4 [Doos 5 [] 3, Doos 6 [] 4] 0, Bol 7 [Doos 8 [] 5, Doos 9 [] 6] 0] 0)

Doos 0 [Bol 1 [Doos 2 [] 1,Doos 3 [] 2] 1,Bol 4 [Doos 5 [] 3,Doos 6 [] 4] 3,Bol 7 [Doos 8 [] 5,Doos 9 [] 6] 5] 5
import List

data Doosboom = Doos Int [Bolboom] Int deriving Show
data Bolboom = Bol Int [Doosboom] Int deriving Show
 
getWaardeBol :: Bolboom -> Int
getWaardeBol (Bol id [] w) = w
getWaardeBol (Bol id doosbomen w) =
        let
                (eerste:rest) = sort [getWaardeDoos d | d <- doosbomen]
        in
                eerste

getWaardeDoos :: Doosboom -> Int
getWaardeDoos (Doos id [] w) = w
getWaardeDoos (Doos id bolbomen w) =
        let
                (eerste:rest) = reverse (sort [getWaardeBol b | b <- bolbomen])
        in
                eerste

berekenDoosboom :: Doosboom -> Doosboom
berekenDoosboom (Doos id bomen w) = Doos id [(berekenBolboom b) | b <- bomen] (getWaardeDoos (Doos id bomen w))

berekenBolboom :: Bolboom -> Bolboom
berekenBolboom (Bol id bomen w) = Bol id [(berekenDoosboom d) | d <- bomen] (getWaardeBol (Bol id bomen w))

alternatieve oplossing

data Maxboom = Max Int [Minboom] Int deriving Show
data Minboom = Min Int [Maxboom] Int deriving Show

complete_max::Maxboom -> Maxboom
------------------------------
complete_max (Max id minbomen w) =
	if w /= -1
		then (Max id minbomen w)
		else let completed_minbomen = map complete_min minbomen
			 max_w = maximum [winst | (Min id maxbomen winst) <- completed_minbomen]
		     in (Max id completed_minbomen max_w)
		
complete_min::Minboom -> Minboom
--------------------------------
complete_min (Min id maxbomen (-1)) =
	let completed_maxbomen = map complete_max maxbomen
	    min_w = minimum [winst | (Max id minbomen winst) <- (map complete_max maxbomen)]
	in (Min id completed_maxbomen (minimum [winst | (Max id minbomen winst) <- (map complete_max maxbomen)]))
complete_min (Min id maxbomen w) = (Min id maxbomen w) 

--Beau 17 jun 2006 17:34 (CEST)