2

On page 57 of the book "Programming Erlang" by Joe Armstrong (2007) 'lists:map/2' is mentioned in the following way:

Virtually all the modules that I write use functions like lists:map/2 —this is so common that I almost consider map to be part of the Erlang language. Calling functions such as map and filter and partition in the module lists is extremely common.

The usage of the word 'almost' got me confused about what the difference between Erlang as a whole and the Erlang language might be, and if there even is a difference at all. Is my confusion based on semantics of the word 'language'? It seems to me as if a standard module floats around the borders of what does and does not belong to the actual language it's implemented in. What are the differences between a programming language at it's core and the standard libraries implemented in them?

I'm aware of the fact that this is quite the newby question, but in my experience jumping to my own conclusions can lead to bad things. I was hoping someone could clarify this somewhat.

Sonder
  • 117
  • 2
  • 9
  • 2
    There's always a difference between *the language* (what defines the syntax, the special keywords etc.) and the *standard library* (the functions which are typically available by default). Depending on the language these two things may be more or less intertwined. Typically you can separate these two; you could have Erlang, the language, without the `lists` module. – deceze May 24 '16 at 01:59
  • Thanks for the quick reply and clarification. Should I delete this question, to keep StackOverflow a little cleaner? I cannot imagine it will be of much help to others. – Sonder May 24 '16 at 02:26
  • 2
    Meh... this isn't exactly the worst question ever, it could actually spawn interesting answers. Perhaps you could rewrite it a little with your new knowledge to invite answers talking in more detail about the difference between a standard library and a language...? – deceze May 24 '16 at 02:38
  • Allright, did edit my question into something more general. However, even though I couldn't find them, I have a feeling this became a duplicate now. – Sonder May 24 '16 at 03:39

1 Answers1

1

Consider this simple program:

1> List = [1, 2, 3, 4, 5].
[1,2,3,4,5]
2> Fun = fun(X) -> X*2 end.
#Fun<erl_eval.6.50752066>

3> lists:map(Fun, List).
[2,4,6,8,10]

4> [Fun(X) || X <- List].
[2,4,6,8,10]

Both produce the same output, however the first one list:map/2 is a library function, and the second one is a language construct at its core, called list comprehension. The first one is implemented in Erlang (accidentally also using list comprehension), the second one is parsed by Erlang. The library function can be optimized only as much as the compiler is able to optimize its implementation in Erlang. However, the list comprehension may be optimized as far as being written in assembler in the Beam VM and called from the resulted beam file for maximum performance.

Some language constructs look like they are part of the language, whereas in fact they are implemented in the library, for example spawn/3. When it's used in the code it looks like a keyword, but in Erlang it's not one of the reserved words. Because of that, Erlang compiler will automatically add the erlang module in front of it and call erlang:spawn/3, which is a library function. Those functions are called BIFs (Build-In Functions).

In general, what belongs to the language itself is what that language's compiler can parse and translate to the executable code (or in other words, what's defined by the language's grammar). Everything else is a library. Libraries are usually written in the language for which they are designed, but it doesn't necessarily have to be the case, e.g. some of Erlang library functions are written using C as Erlang NIFs.

Community
  • 1
  • 1
Greg
  • 8,230
  • 5
  • 38
  • 53
  • The last subsection confused me somewhat: I understand that wat belongs to the language itself can be parsed and translated (_directly_) into executable code. For converting the library parts into executable code, the compiler would have to go fetch the part in the library represented by the 'lib keyword', and then convert it into executable code? Or are these library parts already compiled? – Sonder May 24 '16 at 14:00
  • `erlang:spawn/1` is not BIF. See `erlang:is_builtin(erlang, spawn, 1).` although it is auto-imported function from `erlang` module. – Hynek -Pichi- Vychodil May 24 '16 at 17:33
  • 2
    @Sonder, those parts are already compiled. The last subsection is just a non-precise saying that what belongs to the language is its grammar: https://en.wikipedia.org/wiki/Formal_grammar Everything else is the library. The input source file is tokenized by a lexer https://en.wikipedia.org/wiki/Lexical_analysis and then the parser takes the tokens to produce a parse tree, which then is translated to the executable. See the unofficial grammar productions for Erlang: https://github.com/antlr/grammars-v4/blob/master/erlang/Erlang.g4 this is what belongs to the language. – Greg May 24 '16 at 18:17
  • @Hynek-Pichi-Vychodil, thanks for pointing that out. I have corrected the answer to state the exact arity. – Greg May 24 '16 at 18:21