Related
I find modern functional programming languages and the paradigm in general to be very interesting. A lot of functional programming languages are able to generate efficient native code either by using C as an intermediate language or with their own code generators. I'm talking about languages like Haskell, OCaml, LISP or Scheme (SBCL, Chiken, Gambit and etc). But knowing that functional programming languages requires a big runtime library, that is usually implemented in C (for garbage collection for example), I'd like to ask is it really possible to create bare metal code in such language and only using it without the need to go back to C (for example for OS development, or for running native code on embedded device without operating system)? Is there any functional programming language that doesn't depend on runtime (even if it would be a language subset)? Is there any functional language in which I can reimplement the runtime that it needs (for example languages like Ada, D, Nimrod, Pascal has runtimes that are written in the language itself)? What alternative functional languages do I have for bare metal development these days?
Why such hate for C? Functional languages have to use a lower-level language for producing machine-level code.
And you can also write C in a very pure functional way. C offers to the developer the freedom to follow any programming style she wishes.
You can easily follow the functional paradigm in C by applying principles that you have learned from other functional languages, i.e.:
No mutation.
No malloc.
Avoid state.
Model in terms of verbs rather than nouns.
Use recursion instead of iteration.
et.c.
There is a book with an intro to Functional C.
I'd like to ask is it really possible to create bare metal code in
such language and only using it without the need to go back to C
Bare metal/machine-level/object code are 1GL (1st generation programming languages) made of binary numbers, represented by 1s and 0s.
Most 3GL or 4GL that use a compiler can generate object code.
Is there any functional programming language that doesn't depend on
runtime (even if it would be a language subset)
Well, every application that runs on top of an OS has to use the OS's API. Nearly all popular OSes are written in C/C++. That includes Windows, Linux, MacOS, Android et.c.
So, when creating an app for an OS, you may have to depend on the OS's runtime environment (that is usually written in C/C++).
Is there any functional language in which I can reimplement the
runtime that it needs (for example languages like Ada, D, Nimrod,
Pascal has runtimes that are written in the language itself)
Yeap, you can write your own runtime library for any language you wish, but that's not an easy task to do.
What alternative functional languages do I have for bare metal
development these days?
I suggest that you choose your favorite functional language (Haskell, F#, Scala or whatever) and just forget about the runtime. There are many and good reason that it is written in C. And AFAIK that isn't going to change anytime soon.
By programming in Scala you use the JVM and with F# the CLR or the WinRT. Maybe these options could help you.
EDIT: If what you really want to ask is if it is possible to write an OS in a functional language, then the answer is yes.
House is an OS written in Haskell. The FFI provides the necessary functionality for (a) calling low-level routines for accessing hardware, and (b) managing memory explicitly.
MirageOS is in OCaml, and there have been OSs implemented in ML.
Generally, any "Turing Complete" programming language can be used to create an operating system, and most functional languages like LISP, Haskell, OCaml, and so on are Turing Complete.
I'm thinking there will be very few (if any) alternatives since the primitives will then be system dependent and your object file needs to have it's own GC. (functional languages need a runtime GC)
You can make your own domain specific language in your favorite functional language, perhaps Scheme, that utilize some sort of assembler, like Sassy, to eventually output raw machine code. But really this is just making your own compiler.
A much simpler option to use C as an intermediate. Many does this since most systems have a C compiler. Devices comes with reference implementations in C.
A different alternative is to make a system like SBCL cross compile to your architecture and use it as the runtime OS of your device.
BTW: SBCL has very little implemented in other languages than Common Lisp. The main part is probably the GC. The GC code could have been implemented in CL, but that would have made debugging far more complex and with a buggy GC you get all sorts of strange behaviour.
Is mathematica a functional programming language? what i intend to ask his that i keep reading that mathematica is a conditional rewrite system. I want to know what is this conditional rewrite system?
I also wanted to know whether we can apply the functional programming concepts like continuous passing style, memoization etc using mathematica language?
Mathematica supports multiple programming paradigms (procedural, functional and even object oriented). You can do CPS, memorization etc using the Mathematica language. But Mathematica is more about getting things done. If you want to learn more about the theory behind functional programming, I suggest using Scheme, Haskell, or ML as your vehicle.
I've been researching declarative languages, and it seems like declarative is just an umbrella term for both logic and functional languages. Or am I wrong? Are there any general-purpose declarative programming languages that can't be classified as either functional or logic(al), and simply "declarative"?
A declarative language requires you to code for what you want to happen rather than in an imperative language where you code how the computation should be done.
In general this means that a declarative language does not allow for side-effects, whereas imperative languages almost require coding with side-effects.
In order for general-purpose languages to be, well, general-purpose they require the ability to code side-effects. It thus make them hard to be declarative.
Languages like F# have a strong basis in functional programming, but have any constructs to allow for OO programming and side-effects. This makes F# a general purpose language but does so by allowing imperative style coding to be mixed in with declarative coding.
Although not entirely impossible, I would suspect that there are no "purely declarative" general purpose programming languages just simply by definition.
I'm a python/c/c++ programmer, I'm using emacs, and I read hackers & painters, and I read SICP, and I'm starting reading practical common lisp, but the problem is, I need a library reference as python documentation do(also with how to communicate with C library), so I can actual using lisp in real life. any links? (current focus on common-lisp)
and, after I can really use lisp, how can I learn from others, and dive into the lisp community?
maybe a total lisp reference contains all, but I didn't find one.
Perhaps you are looking for the Common Lisp HyperSpec?
It covers all of ANSI Common Lisp, but will not tell you "how to communicate with C library" since that is implementation dependent.
The Common Lisp Quick Reference gives you a nice overview of the core language. It can be printed to small booklets.
For things like communication to C etc. you need to use the manuals of the libraries (say, CFFI or the specific FFI of the CL implementation) or implementations.
Which Lisp implementation(s) are you using?
CLISP Implementation Notes
SBCL manual
full LispWorks manuals in HTML, PDF and PS
full Allegro CL manuals
I understand very clearly the difference between functional and imperative programming techniques. But there's a widespread tendency to talk of "functional languages", and this really confuses me.
Of course some languages like Haskell are more hospitable to functional programming than other languages like C. But even the former does I/O (it just keeps it in a ghetto). And you can write functional programs in C (it's just absurdly harder). So maybe it's just a matter of degree.
Still, even as a matter of degree, what does it mean when someone calls Scheme a "functional language"? Most Scheme code I see is imperative. Is it just that Scheme makes it easy to write in a functional style if you want to? So too do Lua and Python. Are they "functional languages" too?
I'm (really) not trying to be a language cop. If this is just a loose way of talking, that's fine. I'm just trying to figure out whether it does have some definite meaning (even if it's a matter-of-degree meaning) that I'm not seeing.
Among people who study programming languages for a living, "functional programming language" is a pretty weakly bound term. There is a strong consensus that:
Any language that calls itself functional must support first-class, nested functions with lexical scoping rules.
A significant minority also reserve the term "functional language" for languages which are:
Pure (or side-effect-free, referentially transparent, see also)
as in languages like Agda, Clean, Coq, and Haskell.
Beyond that, what's considered a functional programming language is often a matter of intent, that is, whether is designers want it to be called "functional".
Perl and Smalltalk are examples of languages that support first-class functions but whose designers don't call them functional. Objective Caml is an example of a language that is called functional even though it has a full object system with inheritance and everything.
Languages that are called "functional" will tend to have features like the following (taken from Defining point of functional programming):
Anonymous functions (lambda expressions)
Recursion (more prominent as a result of purity)
Programming with expressions rather than statements (again, from purity)
Closures
Currying / partial application
Lazy evaluation
Algebraic data types and pattern matching
Parametric polymorphism (a.k.a. generics)
The more a particular programming language has syntax and constructs tailored to making the various programming features listed above easy/painless to express & implement, the more likely someone will label it a "functional language".
I would say that a functional language is any language that allows functional programming without undue pain.
I like #Randolpho's answer. With regards to features, I might cite the list here:
Defining point of functional programming
namely
Purity (a.k.a. immutability, eschewing side-effects, referential transparency)
Higher-order functions (e.g. pass a function as a parameter, return it as a result, define anonymous function on the fly as a lambda expression)
Laziness (a.k.a. non-strict evaluation, most useful/usable when coupled with purity)
Algebraic data types and pattern matching
Closures
Currying / partial application
Parametric polymorphism (a.k.a. generics)
Recursion (more prominent as a result of purity)
Programming with expressions rather than statements (again, from purity)
The more a particular programming language has syntax and constructs tailored to making the various FP features listed above easy/painless to express & implement, the more likely someone will label it a "functional language".
Jane Street's Brian Hurt wrote a very good article on this a while back. The basic definition he arrived at is that a functional programming language is a language that models the lambda calculus. Think about what languages are widely agreed to be functional and you'll see that this is a very practical definition.
Lisp was a primitive attempt to model the lambda calculus, so it fits this definition — though since most implementations don't stick very closely to the ideas of lambda calculus, they're generally considered to be mixed-paradigm or at best weakly functional.
This is also why a lot of people bristle at languages like Python being called functional. Python's general philosophy is unrelated to lambda calculus — it doesn't encourage this way of thinking at all — so it's not a functional language. It's a Turing machine with first-class functions. You can do functional-style programming in Python, yes, but the language does not have its roots in the same math that functional languages do. (Incidentally, Guido van Rossum himself agrees with this description of the language.)
A language (and platform) that promotes Functional Programming as a means of fully leveraging the capabilities of the said platform.
A language that makes it a lot harder to create functions with side effects than without side effects. The same counts for mutable/immutable data structures.
I think the same question can be asked about "OOP languages". After all, you can write object oriented programs in C (and it's not uncommon to do so). But C doesn't have any built-in language constructs to enable OOP. You have to do OOP "by hand" without much help from the compiler. That's why it's usually not considered an OOP language. I think this distinction can be applied to "functional languages", too: For example, it's not uncommon to write functional code in C++ (think about STL functions like std::count_if or std::transform). But C++ (for now) lacks built-in language features that enable functional programming, like lambdas. (Let's ignore boost::lambda for the sake of the argument.)
So, to answer your question, I'd say although it's possible to write function programs in each of these languages:
C is not a functional language (no built-in functional language constructs)
Scheme, Python and friends have functional constructs, so they're functional languages. But they also have imperative and OOP constructs, so they're usually referred to as "multi-paradigm" languages.
You can do functional style programming in any language. I try as much as possible.
Python, Linq all promote functional style programming.
A pure functional language like Haskell requires you to do all your computations using mathematical functions, functions that do not modify anything, they just return values.
In addition, functional languages typically allow you to write higher order functions, functions that take functions as arguments and/or return types.
Haskell for one have different types for functions with side-effects and those without.
That's a pretty good discriminating property for being a 100% functional language, at least IMHO.
I wrote a (pretty long) analysis once on why the term 'functional programming language' is meaningless which also tries to explain why for instance 'functions' in Haskell are completely different from 'functions' in Lisp or Python: http://blog.nihilarchitect.net/archives/289/on-functional-programming/
Things like 'map' or 'filter' are for a large part also implementable in C for instance.