-1

I am trying to write a function render by using toPosition with

render :: Dimensie -> Figure a -> [[a]] 

But I can't figure out how. Could you propose a way to do this?

type Figure a = Pos -> a

type Pos = (Double, Double) -- (x, y)

chessboard :: Figure Bool
chessboard (x, y) = even (round x) == even (round y)

type Dimensie = (Int, Int)

render :: Dimensie -> Figure a -> [[a]] 
render  = undefined

toPosition :: Dimensie → (Int, Int) → Pos
toPosition d (x, y) = (fromIntegral x ∗ 2 / b − 1, 1 − fromIntegral y ∗ 2 / h)
        where
           b = fromIntegral (fst d − 1)
           h = fromIntegral (snd d − 1)

And when I call it with

 Figure> render (3,3) chessboard

it should give

[[True,False,True],[False,True,False],[True,False,True]]

And then I want to follow this up by writing a function that gives a certain character for a certain boolean value. It should display for example @ for True and . for False

boolChar :: Bool -> Char
boolChar = undefined

But how do I write this? I don't get how to get a [[Bool]] out of [[a]] at the render function, it keeps telling me that it couldn't match expected type ‘a’ with actual type ‘Bool’ and that ‘a’ is a rigid type variable bound by the type signature for: and then it tells which line it went wrong. So what do I write for render d (x,y) c = .... to get it to display the asked outcome?

dopamane
  • 1,315
  • 2
  • 14
  • 27

1 Answers1

0

Here's a few hints:

  • Write a function to generate a table of Pos with all the coordinates, i.e. a [[Pos]] like this

    generateTable (i,j) = [[(1,1),(1,2),...,(1,j)], ..., [(i,1),...,(i,j)]]
    
    • To solve this you might want to write an auxiliary function generateRow x j = [(x,1),...,(x,j)] first, and then use it to generate each row
    • You can use recursion, or you might also use a (possibly nested) list comprehension. There are many options.
  • Once you have your table :: [[Pos]] you can map (map figure) table to get the wanted [[a]].

    • Alternatively, you can use the figure function sooner, and avoid to generate the pairs in the first place.
chi
  • 111,837
  • 3
  • 133
  • 218