Guards vs. functions with pattern matching

2

I'm starting at Haskell and doing her exercises on Exercism . When I sent a code, I was comparing it with others and I came across a similar one to mine, but I used a definition of guards (I do not know if it is guard I saw in a place calling function by pattern matching , but not sure) different:

-- Minha função
transcribe :: Char -> Maybe Char
transcribe nucleotide
    | nucleotide == 'G' = Just 'C'
    | nucleotide == 'C' = Just 'G'
    | nucleotide == 'T' = Just 'A'
    | nucleotide == 'A' = Just 'U'
    | otherwise         = Nothing

-- Função de outro participante
transcribe :: Char -> Maybe Char
transcribe 'G' = Just 'C'
transcribe 'C' = Just 'G'
transcribe 'T' = Just 'A'
transcribe 'A' = Just 'U'
transcribe _   = Nothing

So I'd like to understand, is there any difference between the two forms? For cases like this, which one is the most correct? Or are the two in the end the same?

    
asked by anonymous 19.01.2018 / 22:30

1 answer

3

First, your role is to use guards ), while that of the other participant uses pattern matching ( > patter matching ). In addition, both will always have the same results and are semantically the same.

Choosing to write in one way or another is a matter of style or for ease of reading. In this case, since the comparison of each guard is only equality in all cases ( nucleotide == ... ), the most common is to choose marriage patterns, even though it does not make any difference.

You may want to read a little about pattern matching:

20.01.2018 / 18:45