1

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?

6
  • 4
    First, you should put a complete working example: What's a cell? What's GameState? With this code, is impossible to understand what's happening beside a type error on your function movePawnBack. Commented Feb 12, 2016 at 1:48
  • First of all you have a syntax error BP = findCells g E this is invalid, there is no assignment or mutation or whatever you are trying to do there. Secondly what is E, WP, BP and where did they come from? Commented Feb 12, 2016 at 1:58
  • @AlexeyKuleshevich, I don't think it's a syntax error, per se, but it's clearly a mistake. It means "calculate findCells g E, check if the result is BP, and if it's not, throw an error." Commented Feb 12, 2016 at 2:22
  • @dfeuer I don't think it means (or is intended to mean) that. After all, BP doesn't look at all like a [(Int, Int)], which is the kind of thing findCells returns. But it's very unclear what it is intended to mean! Commented Feb 12, 2016 at 2:55
  • Ok I will put more code up. WK and BP are coming from a different module and E means Empty, So if I have an empty cell I want to fill it, so BP is now in the empty spot Commented Feb 12, 2016 at 3:20

1 Answer 1

1

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?

Sign up to request clarification or add additional context in comments.

1 Comment

Hello sorry was away. I am trying to replace the empty spot with a certain pawn, I will see if that works!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.