[HS] Fractional -> Int

Pagina: 1
Acties:

  • dlmh
  • Registratie: Januari 2000
  • Laatst online: 09-09 15:44
Ik moet voor m'n studie een proggie schrijven in Haskell (lang leve de wiskunde, zullen we maar zeggen) dat met behulp van commandos een tekening op het beeld zet.

De commando's :

Forward x - Om x stappen naar voren te gaan
Back x - Om x stappen naar achteren te gaan
Left x - Om x graden naar links te draaien
Right x - Om x graden naar rechts te draaien
PenUp - Om je pen van het papier te halen (lopen kan dan wel maar er verschijnt geen streep op het beeld
PenDown - Om je pen op het papier te zetten.

Het programma leest de lijst met commando's uit een bestand en voert deze dan sequentieel uit.

Het probleem zit 'm in het berekenen van de richting. Ik gebruik de volgende code voor het uitvoeren van de commando's en het berekenen van de richting. (dx, dy) :
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
doeCommandos :: Window -> Toestand -> [Commando] -> IO ()
doeCommandos w (x, y, r, b) [] = return ()
doeCommandos w (x, y, r, b) (c:cs) = sequence_ [execCommando w (x, y, r, b) c,
                                doeCommandos w (x, y, r, b) cs]

execCommando :: Window -> Toestand -> Commando -> IO ()
execCommando w t (Forward s) = drawInWindow w (tekenLijn t s)
execCommando w t (Back s) = drawInWindow w (tekenLijn t s)

{-
execCommando w (x, y, r, b) cs (Left g) = doeCommandos w (x, y, r-g, b) cs
execCommando w (x, y, r, b) cs (Right g) = doeCommandos w (x, y, r+g, b) cs
execCommando w (x, y, r, b) cs (PenUp) = doeCommandos (x, y, r, False) cs
execCommando w (x, y, r, b) cs (PenDown) = doeCommandos (x, y, r, True) cs -}

tekenLijn :: Toestand -> Int -> Graphic
tekenLijn (x, y, r, b) s = line (x, y) (dxy x y r s)

dxy :: Int -> Int -> Int -> Int -> (Int, Int)
dxy x y r s = if r <= 90 
        then ((90 - r) * (s/90), r * (s/90))
        else if r <= 180 
           then (r * (s/180), (180 - r) * (s/180))
           else if r <= 270
              then ((270 - r) * (s/270), r * (s/270))
              else (r * (s/360), (360 - r) * (s/360))

Dit geeft bij mij echter een compile error, namelijk dat ik een 'instance of Fractional Int' nodig heb. Dit klopt ook wel want een deling levert (over het algemeent) geen Int op :). Ik heb dus al wat met ceiling, round en truncate zitten proberen, maar geen suc6...

Iemand betere ideeen? :?

“If a cluttered desk is a sign of a cluttered mind, of what, then, is an empty desk a sign?” - Albert Einstein