Electrical Engineering Stack Exchange is a question and answer site for electronics and electrical engineering professionals, students, and enthusiasts. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Functional languages like Haskell, LISP, or Scheme allow a programmer to work quickly using the functional programming paradigm. They do have their inefficiencies, but my application places greater emphasis on programmer efficiency than efficiency of the program itself.

I'd like to use functional programming on a microcontroller to do machine control, etc.

What limitations are there, such as minimum system resources?
What example implementations of these languages are available?

share|improve this question
If your question is "Isn't it worth it to program any machine with the most powerful programming language you can get your hands on," the C++ and Java questions are recommended reading (about OOP rather than functional programming). – Kevin Vermeer Jan 27 '11 at 17:45
Your first paragraph comes across as argumentative, which has netted you a few close votes. Consider rewording to something more passive ("I'm interested in using functional programming for machine control, what examples are there of Haskell/LISP/Scheme implementations for embedded systems") or removing it entirely. – Kevin Vermeer Jan 27 '11 at 17:46
I don't buy your "inefficient" statement. You seem to exhibit extreme bias towards the hobbyist/prototype side--low volume (aka: 1). C/C++/asm results in smaller, faster code that is amplified thousands or millions of times when you can use processors with just enough speed and space. Embedded is embedded. You are not programming on a general-purpose OS. – Nick T Jan 27 '11 at 17:51
@Nick T - "C/C++/asm results in smaller, faster code that is amplified thousands or millions of times when you can use processors with just enough speed and space" - what about maintenance? A functional language can frequently do in a single line what a C-program requires 10s to do, meaning less room for bugs. In addition, they can be complied (ie Haskell), and made to run on the target, which is faster than interpreters. I wanted to explore this topic a bit because a compiled-Haskell might be just as fast, but quicker to develop than say a C app. Wanted to question the status quo a bit. – J. Polfer Jan 27 '11 at 21:28
@Sheepsimulator Unfortunately, comments like your last one make questions like this argumentative. – Kellenjb Jan 27 '11 at 21:39

ARMPIT SCHEME is an interpreter for the Scheme language (lexically-scoped dialect of Lisp) that runs on RISC microcontrollers with ARM core. It is based on the description in the Revised Report on the Algorithmic Language Scheme (r5rs), with some extensions (for I/O) and some omissions (to fit within MCU memory). It is further designed to support multitasking and multiprocessing. Armpit Scheme is expected to be well suited to educational settings, including student projects in courses on control and instrumentation, or capstone design courses where microcontrollers are needed. It is meant to enrich the spectrum of interpreted languages available for MCUs (eg. BASIC and FORTH) and can be an alternative to MCU-based bytecode interpreters (eg. for Scheme or Java) and to compiled languages (eg. C).


You say:

Using C, C++, assembly, etc. is quite inefficient compared to languages like Haskell, LISP, or Scheme

Using high level languages is a more efficient use of programmer time, but can often be a less efficient use of computing resources. For embedded systems manufactured in volume, cost and performance are often higher priority than development effort.

share|improve this answer

C, C++, and Assembly are all very close to machine language. By using a higher level language, you are adding additional overhead in exchange for more rapid/easier/etc development.

share|improve this answer
-1: I don't really agree with this answer. Although your right about Assembly being close to machine language, C and C++ are very different high level languages. – BG100 Jan 27 '11 at 17:37
@BG100, I'd actually draw the "high level/low level" line somewhere within C rather then just call it a high-level language. When performing arithmetic, pointer (string) operations, and other common basic tasks, the instructions that compilers generally produce have the CPU directly manipulating the data without any layers of abstraction. – Nick T Jan 27 '11 at 21:58
@Nick T: I see your point, but consider this: if you write an interrupt routine that generally needs to execute as quickly as possible, in C you would have no idea how long it would take to run, but in assembler you would can just count the instructions. I think low level is knowing EXACTLY was is going on in your program, you don't know this for sure if your using C. – BG100 Jan 27 '11 at 22:33
@BG100: the same assembler instruction can take different numbers of cycles to execute based on operands and their addressing modes. Though in C, once you compile you get static code that does not (can not) change. True, this is a somewhat tenuous argument, but if we're going to argue the minutiae to try to draw a big red line... – Nick T Jan 28 '11 at 3:29

You also can program AVR controllers with Haskell using Atom/Copilot, for example http://leepike.wordpress.com/2010/12/18/haskell-and-hardware-for-the-holidays/

share|improve this answer
Haskell really is good stuff <3 – Trygve Laugstøl Dec 11 '12 at 16:43

The majority of microcontrollers are still 8 and 16-bit devices (although this is slowly changing). The two instances of higher-level languages (Scheme and Python) mentioned in other answers so far are both running on 32-bit ARM cores. The smaller 8 and 16-bit devices (which may cost only a couple of dollars) don't have enough RAM to support the languages being mentioned -- typically they only have a few KB of RAM.

Also, these higher-level languages are not designed for writing low latency interrupt handlers and the like. It's not unusual for a microcontroller interrupt handler to get called hundreds or thousands of times per second, and each time required to perform its task in tens of microseconds or less.

share|improve this answer
Scheme was developed in the mid-late '70's and very early '80's. By no means does Scheme require a 32-bit processor or megabytes of memory. Scheme was available for AT-class PCs in the mid-80's. Recent implementations might be optimized for more resource-rich environments, but there's clear examples of Schemes that run on what are nowadays "miniscule" computing platforms. – The Photon Dec 11 '12 at 20:10
@ThePhoton I stand corrected. Although I was aware of the BIT project, which targets processors with tens of KB of memory (more than what is available on most small microcontrollers), I just discovered PICBIT, designed by a couple of students at the Université de Montréal and Université Laval, which allows real Scheme programs to run on PIC processors with as little as 2K of RAM. Pretty amazing. – tcrosley Dec 12 '12 at 12:28

"Are there ways to do functional programming with a functional language on an MCU to solve difficult problems?"

Yes, there are ways. But the downside is you need a 32-bit processor, MMU, 128MB RAM, SSD, an RTOS, and $$$.

Microcontrollers are different than microprocessors. The microcontroller may only be a 8-bit CPU, 1K RAM, 8K ROM, but it has a built in UART, PWM, ADC, etc. And it only costs $1.30.

So you could have all that high-level languages running, but it costs a lot more to make.

share|improve this answer
I think you need to revisit your definition of microcontroller. Many microcontrollers now have 128kB or more of Flash, and 64kB or more of RAM, plenty of space to run an interpreter for some smallish languages. It looks like you're giving specs for an embedded Linux device; I think the OP was asking for a dedicated port. – Kevin Vermeer Jan 28 '11 at 15:52
If you are paying $1.30 for a 8-bit MCU, then there are several 32-bit MCUs that are cheaper. Also, take in account that most 8-bit MCUs on the market are horribly code-ineffective architectures, with designs inherited from the early 80s. – Lundin Dec 12 '12 at 7:57

It's possible to do some functional programming with the Lua language. Really, Lua is a mutli-paradigm language; Wikipedia claims that it's a 'scripting, imperative, functional, object-oriented, prototype-based' language. The language doesn't enforce a single paradigm, but instead is flexible enough to allow the programmer to implement whatever paradigm is applicable to the situation. It's been influenced by Scheme.

Lua's features include first-class functions, lexical scoping and closures and coroutines, which are useful for functional programming. You can see how these features are used on the Lua users wiki, which has a page dedicated to functional programming. I also came across this Google Code project, but I haven't used it (it does claim to be influenced by Haskell, another language which you mentioned).

eLua is an implementation which is available configured for a number of development boards for the ARM7TMDI, Cortex-M3, ARM966E-S and AVR32 architectures, and is open-source so you can configure it for your own platform. Lua is implemented in ANSI C and the entire source weighs in at under 200kB, so you should be able to build it for most platforms with a C compiler. At least 128k of Flash and 32k of RAM is recommended. I'm working on a PIC32 port for it (still in the 'Get the PIC32 board' stage, though) at the moment.

The great thing about Lua is that it was designed as a glue language, so it's very easy to write C extensions for the stuff that needs to be fast (like interrupts etc), and use the dynamic, interpreted features of the language to do rapid development in the program logic.

Lua isn't a purely functional language, but you can do a lot of functional programming in it, it's fast and small (compared to other scripting languages), and you don't need to reflash your device to try out a program. There's even an interactive interpreter!

share|improve this answer

I've been programming an ARM board in Python recently, and I think it's great. It's no good for real-time control, but I'm doing more web-related stuff, which is far more pleasant in a high-level language than in C.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.