0

I'm an intermediate Haskell programmer (if you measure by how long I've been using it, not necessarily by skill :-) ).

I often feel like a newbie.

In looking through the Haskell Gloss sourcecode I found some syntax I'm not familiar with.

I constructed an MWE below. 'test' is the function in question.

I looked up the list of Haskell operators and it says that '<-' is only used in list comprehensions and inside a do block. This seems like neither.

It doesn't seem like a Guard since they use '=' signs not '<-'.

It works, but what syntax is this?

Thanks in advance

module Main where

data Point = P Float Float

test :: Point -> Point -> Float
test pA pB 
     | (P a b) <- pA
    ,  (P c d) <- pB
     = a + b + c + d 

main :: IO()

main = do

 let x = test (P 3 11) (P 20 8)
 putStrLn $ show x

-- Yields 42.0

TomP
  • 108
  • 6
  • Is the acute accent (`´`) a typo? – mkrieger1 Nov 26 '20 at 19:21
  • 3
    It's called a patter guard. It's not a very commonly used construct in GHC Haskell, but sometimes it's useful to have. In your specific example, however, it's a bit useless since one could have written `test (P a b) (P c d) = a+b+c+d` instead, which is short and clear. – chi Nov 26 '20 at 19:31
  • 1
    Thankjs everyone for the quick response! Haskell is a continuous learning experience for me. – TomP Nov 26 '20 at 19:55
  • My example was a contrived to be an MWE and it is a silly way to do it in real life. – TomP Nov 26 '20 at 19:58

0 Answers0