r/Idris • • Jun 06 '21

What's wrong with this piece of code?

I'm trying to find the point in the left bottom corner.

data Point = MkPoint Double Double

findInitial : List Point -> Maybe Point
findInitial [] = Nothing
findInitial (a@(MkPoint x y) :: xs) = do
  (Just b@(MkPoint u v)) <- findInitial xs
               | Nothing => Just a
  case (compare y v, compare x u) of
       (LT, _) => Just b
       (EQ, LT) => Just b
       _ => Just a

However, idris warn me:

Error: While processing right hand side of findInitial. When unifying Point and Maybe ?_.
Mismatch between: Point and Maybe ?_.

But I don't find why the type doesn't match.

Is it because that I shouldn't use <- where there's no IO Monads related?

If that's the point, Is there a way that I can avoid nesting cases?

Thanks a lot!

1 Upvotes

2 comments sorted by

0

u/[deleted] Jun 06 '21

[deleted]

1

u/lyhokia Jun 06 '21

Thanks, I don't know the guards can work with let! Does it acts like a syntax sugar that you can use it anywhere when you pattern matches?

0

u/[deleted] Jun 07 '21

[deleted]

0

u/bss03 Jun 08 '21

anywhere else where you can pattern match

function definition LHS?

But there, | is used for with arguments.