AVC
AVC

Reputation: 61

How to code the output value in Haskell that equals a function?

I have a function that finds empty cells in a chess game I am making, and I want to replace one of my pieces into that empty spot that I found. I have a function for findCells, so I have a code something like this

movePawnBack :: GameState -> Player -> [[Cell]]
movePawnBack g p = if p == Black then BP = findCells g E else WP = findCells g E


findCells :: GameState -> Cell -> [(Int, Int)]
findCells b c = [(x, y) | x <- [0..4], y <-[0..4], getFromBoard (theBoard b) (x, y) == c]

However, it gives me an error that I don't have an else cause and if I do something like this BP ==... then I just return a boolean type which is not what I want. Is there an easier way to do this?

Upvotes: 1

Views: 48

Answers (1)

lehins
lehins

Reputation: 9767

Is that what you are trying to do?

movePawnBack :: GameState -> Player -> (Cell -> [(Int, Int)])
movePawnBack g p = if p == Black then (findCells g) else (findCells g)

In order to return a function, besides currying, as it is done above, you can also use lambda

movePawnBack :: GameState -> Player -> (Cell -> [(Int, Int)])
movePawnBack g p = if p == Black then (\bp -> findCells g bp) else (\wp -> findCells g wp)

Also note the return type (Cell -> [(Int, Int)]), whenever it is in parenthesis, it usually means a function, although it is not required in above case, I put it there because of your question title: output value in Haskell that equals a function?. For instance map takes a function:

>>> :t map
map :: (a -> b) -> [a] -> [b]

So is that what you were looking for?

Upvotes: 1

Related Questions