2

This one of these tricks that when you're unfamiliar with a language is more difficult to find but everybody else knows about and use it.

In my case I wonder what does it mean when you have the name of a variable, say ts, and you put the symbol \ before it:

newtype Parser a = Parser (String -> [(String, a)])
produce :: a -> Parser a
produce x = Parser (\ts -> [(ts, x)])

My guess is this is abstracting the variable? If so what would be its translation to other languages like Scala?

user1868607
  • 2,558
  • 1
  • 17
  • 38
  • 1
    @dfeuer I don't see what relation does my question have with getting started with haskell. of course i'm getting started with haskell but i guess not all people that are beginning with haskell and have specific doubts should in your opinion redirect to "getting started with haskell". please tell me if i'm missing something here – user1868607 Jul 21 '18 at 19:07
  • 1
    @dfeuer the link you asked for is just another example of an identifier that may be weird for newcomers in haskell, i was linking in order to show that this kind of question is legitimate here (over 17 upvotes). I also ask myself why my question is marked as a duplicate and the linked question https://stackoverflow.com/questions/5673916/apostrophe-in-identifiers-in-haskell?rq=1 is not. – user1868607 Jul 21 '18 at 19:09
  • 1
    it would be nice if those who downvoted the question kindly explained what they find wrong with it – user1868607 Jul 21 '18 at 19:11
  • 3
    FYI, I did not downvote your question. I marked this as a duplicate of the canonical dupe target because it is something that will be covered in the first few pages of a typical Haskell tutorial or textbook. Stack Overflow is not a good place to learn the bare basics of a new programming language; there are much better resources available for that. Whether the other question should also be considered a duplicate is a judgement call. It is quite a basic question, but it's more a lexical syntax detail than a fundamental language construct. – dfeuer Jul 21 '18 at 19:16
  • @dfeur alright thanks a lot for clarifying – user1868607 Jul 21 '18 at 19:19
  • it would have been also nice to note that this can be browsed in Hoogle as i learned in this question: https://stackoverflow.com/questions/37286376/what-does-mean-in-haskell – user1868607 Jul 21 '18 at 19:58
  • or even this other https://stackoverflow.com/questions/6824255/whats-the-point-of-map-in-haskell-when-there-is-fmap who shows how to do so in ghci – user1868607 Jul 21 '18 at 20:03
  • 4
    `\\` is not an operator, and can't be found by Hoogle. It's part of the basic syntax of the language. – dfeuer Jul 22 '18 at 03:20

1 Answers1

7

\parameter1 ... parameterN -> expression is Haskell's syntax for lambdas. The Scala equivalent would be (parameter1, ..., parameterN) => expression (or, if we want to maintain the fact that the function is curried, parameter1 => ... => parameterN => expression).

The backslash was chosen for this syntax since it's the ASCII character that looks most like a λ.

sepp2k
  • 363,768
  • 54
  • 674
  • 675