0

I am a strict native languages programmer learning Erlang recently, and I am wondering why people not use c/c++ to directly implement their own functions/modules and run it natively ? And it's not something very difficult to implements even with good scalability. Here's my thoughts:

  1. Any functions in functional programming language take some inputs and generate some outputs. This can be easily done in c/c++ by making small programs (.exe). Each executable has its name in the format of name_version.exe and takes one file as input data and produce another file as output data.

  2. Programming invocation is nothing but making a temporary input file and calling the most updated .exe file and waiting for the result.

  3. Remote function call in function programming language can be implemented by making a server program in each server, accepting requests in the form of (executable path + ' ' + input params), then the server start to look for the latest version of executable in their local copies, invoke it as a new process, and sending back results in TCP stream.

  4. Scalability can be implemented by making a monitor program in each server returning current states (%cpu, %mem) and shooting UDP messages to tell other servers periodically. So that each .exe program can smartly do RPC to the idle servers.

  5. Application update is as easy as copying a new .exe files with the increasing version number. And old .exe files won't be deleted, or can be deleted manually.

So overall, function programming seems to be something to solve the scalability problem. And scalability problem often arise in the situation that one machine has some certain bottleneck on CPU, mem, harddisk,... etc. However, if function programming is not efficient enough, it won't actually be a true solution to scalability problem. Just think of a native program runs 20x faster than a program written in a function programming language, and use 10x less memory. And it's similar to say that you have 20 machines running together, but all these machines can't even beat a single machine that running the native code.

DU Jiaen
  • 955
  • 6
  • 14
  • 1
    Is your question why people don't use C/C++? Some people never learn it, or they simply prefer higher level languages. Functional programming has its own set of benefits just as OOP... – OneCricketeer Nov 09 '15 at 07:17
  • Yes. my question is why people don't use c/c++ or other native stuff rather than function programming languages. – DU Jiaen Nov 09 '15 at 07:19
  • 5
    Many people consider Javascript functional. You can't use C/C++ to replace it. Your question might as well be "why doesn't everyone program in Assembly". It's very apples and oranges. – OneCricketeer Nov 09 '15 at 07:26
  • 1
    I don't know erlang, but have you actually measured your solution against an equivalent Erlang program? I can almost guarantee that creating a separate binary for each function will NOT scale. – MikeMB Nov 09 '15 at 07:30
  • why do you think it won't scale ? – DU Jiaen Nov 09 '15 at 08:02
  • 2
    This is really more a philosophical and business question than anything else. No matter how easy you might want to suggest it is to develop with C / C++, the truth is that developing in higher level languages like C takes longer, often A LOT longer. In a world where ideas are top when they are hot and stop when they are not, and there is a rush to get good software to market and capitalise before anyone else, in a world where good software development time costs hugely more than more servers required to scale sidewards, higher level languages will win all day long. – Michael Nov 09 '15 at 08:29
  • 3
    It's difficult to think of a *less* efficient solution than your suggestion. Creating two files and launching a process takes thousands, if not millions, of times longer than calling an Erlang function, even remotely. You really don't want to do that in a system that has any performance requirements at all. – molbdnilo Nov 09 '15 at 08:44
  • You're right. lauching a process is slow. But if you see the Erlang tutorial, http://www.erlang.org/course/concurrent_programming.html, it's lauching process (with new pid) as well. I tested with Ubuntu 10.04, in one second you can launch 400 process and wait for it finished sequentially, with one thread. I am not saying this is my ultimate solution, but things can be surely optimized, for example, using .dll instead of .exe and preload it at the begnning, using TCP pipe instead of file. After all, I just want to say it should not be difficult to make it native. – DU Jiaen Nov 09 '15 at 09:52
  • 1
    @DUJiaen The "process" you spawn in Erlang isn't a process in the operating system; it's an Erlang-specific thing, and by default it runs in the same OS process as the spawner. Spawning an Erlang process is fast, but you don't usually do it often anyway, but send messages to it instead. And 400 empty function calls/second was a lot about half a century ago. You'll need to reach millions if you want to do anything useful. – molbdnilo Nov 09 '15 at 12:20
  • Related: [Erlang Process vs Java Thread](http://stackoverflow.com/questions/32294367/erlang-process-vs-java-thread/32296577#32296577) Also, [The actor model: Why is erlang special? Or, why do you need another language for it?](http://stackoverflow.com/questions/8107612/the-actor-model-why-is-erlang-special-or-why-do-you-need-another-language-for) – zxq9 Nov 09 '15 at 12:45
  • 1
    Well, Haskell is native indeed – Lol4t0 Nov 09 '15 at 18:34

1 Answers1

3

Excerpt from Why Functional Programming Matters by John Hughes:

As software becomes more and more complex, it is more and more important to structure it well. Well-structured software is easy to write, easy to debug, and provides a collection of modules that can be re-used to reduce future programming costs. Conventional languages place conceptual limits on the way problems can be modularised. Functional languages push those limits back. In this paper we show that two features of functional languages in particular, higher-order functions and lazy evaluation, can contribute greatly to modularity. As examples, we manipulate lists and trees, program several numerical algorithms, and implement the alpha-beta heuristic (an algorithm from Artificial Intelligence used in game-playing programs). Since modularity is the key to successful programming, functional languages are vitally important to the real world.

Also check this one: Functional programming and non-functional programming

Even better link by @zxq9: The actor model: Why is erlang special? Or, why do you need another language for it?

Community
  • 1
  • 1
aronisstav
  • 7,755
  • 5
  • 23
  • 48