Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is being passed in?

Tags:

syntax

haskell

In the code:

oneChar :: Char -> Doc
oneChar c = case lookup c simpleEscapes of
              Just r -> text r
              Nothing | mustEscape c -> hexEscape c
                       | otherwise   -> char c
    where mustEscape c = c < ' ' || c == '\x7f' || c > '\xff'

simpleEscapes :: [(Char, String)]
simpleEscapes = zipWith ch "\b\n\f\r\t\\\"/" "bnfrt\\\"/"
    where ch a b = (a, ['\\',b])

r isn't being passed to oneChar. Where does r come from?

like image 518
Delirium tremens Avatar asked Jun 04 '10 21:06

Delirium tremens


2 Answers

lookup c simpleEscapes returns a Maybe String value, which can be either Nothing or Just <a string>. r is the string contained in the Just, as defined by the line:

Just r -> text r
like image 137
John Millikin Avatar answered Sep 21 '22 16:09

John Millikin


The case keyword introduces a pattern match, which has the form case EXPR of (PATTERN -> EXPR)+. So Just r is a pattern, that matches the result of lookup c simpleEscapes of. In a pattern, variables can be bound. Basically this means, if lookup c simpleEscapes of returns a Just then r will be bound to the value inside that Just and the result of the expression will be text r.

like image 28
sepp2k Avatar answered Sep 22 '22 16:09

sepp2k