2

I have the following data which can have a Ship or not:

data LaserCollisionResult = NoCollision | LaserToLaserCollision Ship | LaserToShipCollision Ship deriving (Eq, Show)

then, later on, I am trying to check if a LaserCollisionResult is of type LaserToLaserCollision, but I get an error. My [lambda] function is this:

laserPaths' = map (\(p,r) -> if r == LaserToLaserCollision then doSomethingWith p else p) $ zip laserPaths laserCollisionResults

The error I am getting is:

Couldn't match type 'LaserCollisionResult' with 'Ship -> LaserCollisionResult'
Expected type: [Ship -> LaserCollisionResult]
Actual type: [LaserCollisionResult]
In the second argument of 'zip', namely laserCollisionResults.

How can I check whether a LaserCollisionResult in laserCollisionResults is of type LaserToLaserCollision?

Mickael Bergeron Néron
  • 1,472
  • 1
  • 18
  • 31
  • 2
    `map f $ zip xs ys` is the same as `zipWith (curry f) xs ys`. You can simplify `laserPaths'` to `zipWith (\p r -> case r of {...}) laserPaths laserCollisionResults`. – Cirdec Feb 14 '15 at 10:23

2 Answers2

5

Replace your lambda by

(\(p,r) -> case r of {LaserToLaserCollision _ -> doSomethingWith p; _ -> p})

By the way, for this you don't need to derive an Eq instance.

2

You need to match on r e.g.

laserPaths' = map (\(p,r) -> if isLaserCollision r then doSomethingWith p else p) $ zip laserPaths laserCollisionResults
  where isLaserCollision (LaserToLaserCollision _) = True
        isLaserCollision _ = False

or you can match inline:

 laserPaths' = map (\(p, r) -> case r of { (LaserToLaserCollision _) -> doSomethingWith p ; _ -> p}) $ zip laserPaths laserCollisionResults
Lee
  • 142,018
  • 20
  • 234
  • 287