0

I was reading a book on Compiler Design and there's a part in it which says,

The main difference in the compilers of different languages is concerned with the code generated by them. In general, it can be said that the compilers for imperative and object oriented languages generate the code in assembly language; whereas functional language compilers generate in C or C++.

Is the last part about C/C++ correct? I couldn't find anything through a Google search so I am asking here.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Stupid Man
  • 885
  • 2
  • 14
  • 31
  • 1
    Yes, C is a popular intermediate format for higher-level languages because there are C compilers for every architecture. From http://yosefk.com/blog/c-as-an-intermediate-language.html: "*It's extremely portable, it compiles in a snap, optimizes nicely, and you get interoperability with loads of stuff.*" – Bergi Nov 20 '16 at 21:43
  • 2
    It's [not only functional languages](http://stackoverflow.com/questions/6498850/programming-languages-that-compile-into-c-c-source). However, languages that use the functional paradigm are typically higher-level so you'll more likely find them in that category. – Bergi Nov 20 '16 at 21:45
  • 2
    See also the cross-site duplicate [When does it make sense to compile my own language to C code first?](http://softwareengineering.stackexchange.com/q/257868/66652) at [SoftwareEngineering.SE] – Bergi Nov 20 '16 at 21:45
  • @Bergi My textbook has lots of other factual inaccuracies so I was a bit skeptical about it. Thanks for these links. – Stupid Man Nov 20 '16 at 22:03
  • 1
    How old is this book? It used to be closer to accurate twenty years ago than it is now. – ibid Nov 21 '16 at 05:13
  • @ibid It's a new book published last year but you wont find it anywhere except India. It's written for clearing university exams and most of the content is just lots of technical terms and definitions copied verbatim from Wikipedia and other sources. – Stupid Man Nov 21 '16 at 07:24

1 Answers1

6

Is the last part about C/C++ correct?

No, it's not.

Some languages do compile to C, but that's not specific to functional languages. If this is more common among functional languages than non-functional ones, it certainly isn't so to the extend that one might call this a property of functional languages.

Further it's definitely not the case that most real-world functional programming languages are compiled to C (by their official and/or most used implementation). If I list the first ten functional programming languages/implementations I can think of (Haskell (GHC, Hugs), OCaml, F#, Scala, Clojure, Racket, Common Lisp (SBCL, CLisp)), none of them compile to C (to the best of my knowledge). GHC did have a C backend at one point, but that's been discontinued.

Two languages, that I can think of, whose primary implementations compile to C are Vala and Haxe, neither of which are functional languages. In one of the linked threads, Chicken Scheme was mentioned. So that's one relatively well-known implementation that uses C. That's hardly enough to justify the claim that this is "generally" the case.

sepp2k
  • 363,768
  • 54
  • 674
  • 675