39

I've played around with a few functional programming languages and really enjoy the s-expr syntax used by Lisps (Scheme in particular).

I also see the advantages of working in a purely functional language. Therefore:

Are there any purely functional Schemes (or Lisps in general)?

Don Stewart
  • 137,316
  • 36
  • 365
  • 468
nickname
  • 1,187
  • 1
  • 9
  • 20

9 Answers9

26

The new Racket language (formerly PLT Scheme) allows you to implement any semantics you like with s-expressions (really any syntax). The base language is an eagerly evaluated, dynamically typed scheme variant but some notable languages built on top are a lazy scheme and a functional reactive system called Father Time.

An easy way to make a purely functional language in Racket is to take the base language and not provide any procedures that mutate state. For example:

#lang racket/base
(provide (except-out (all-from-out racket/base) set! ...more here...))

makes up a language that has no set!.

Eli Barzilay
  • 29,301
  • 3
  • 67
  • 110
jonr
  • 1,133
  • 1
  • 11
  • 25
16

I don't believe there are any purely functional Lisps, but Clojure is probably the closest.

Rich Hickey, the creator of Clojure:

Why did I write yet another programming language? Basically because I wanted a Lisp for Functional Programming designed for Concurrency and couldn't find one.

http://clojure.org/rationale

Clojure is functional, with immutable data types and variables, but you can get mutable behavior in some special cases or by dropping down to Java (Clojure runs on the JVM).

This is by design - another quote by Rich is

A purely functional programming language is only good for heating your computer.

See the presentation of Clojure for Lisp programmers.

j-g-faustus
  • 8,801
  • 4
  • 32
  • 42
  • 9
    "A purely functional programming language is only good for heating your computer". LOL! – J D Dec 25 '10 at 12:41
  • 4
    But heating the computer is a side-effect too. – coredump Aug 04 '16 at 16:54
  • @coredump When we extend this concept further, there cannot be any purely functional thing doing anything besides existing, because any movement would create some heat as a result of a physical process in the real world ^_^ But then again the fact that it exists has side effects too ... – Zelphir Kaltstahl Sep 23 '16 at 09:28
13

Are there any purely functional Schemes (or Lisps in general)?

The ACL2 theorem prover is a pure Lisp. It is, however, intended for theorem proving rather than programming, and in particular it is limited to first-order programs. It has, however, been extremely successful in its niche. Among other things, it won the 2005 ACM Software System Award.

Norman Ramsey
  • 198,648
  • 61
  • 360
  • 533
12

Probably not, at least not as anything other than toys/proofs of concept. Note that even Haskell isn't 100% purely functional--it has secret escape hatches, and anything in IO is only "pure" in some torturous, hand-waving sense of the word.

So, that said, do you really need a purely functional language? You can write purely functional code in almost any language, with varying degrees of inconvenience and inefficiency.

Of course, languages that assume universal state-modification make it painful to keep things pure, so perhaps what you really want is a language that encourages immutability? In that case, you might find it worthwhile to take a look at Clojure's philosophy. And it's a Lisp, to boot!

As a final note, do realize that most of Haskell's "syntax" is thick layers of sugar. The underlying language is not much more than a typed lambda calculus, and nothing stops you from writing all your code that way. You may get funny looks from other Haskell programmers, though. There's also Liskell but I'm not sure what state it's in these days.

On a final, practical note: If you want to actually write code you intend to use, not just tinker with stuff for fun, you'll really want a clever compiler that knows how to work with pure code/immutable data structures.

C. A. McCann
  • 76,893
  • 19
  • 209
  • 302
  • 15
    "anything in IO is only "pure" in some torturous, hand-waving sense of the word". Conal is controversial. A much cleaner description relies on realizing that IO describes actions, and a clear denotation can be made: http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.13.9123 – Don Stewart May 23 '10 at 01:38
  • Thanks for the response. As I said above, I don't actually care about how "productive" the language is in the traditional sense. In general, I'm only looking for a purely functional language because I would like to play with the possibilities. Nothing more! – nickname May 23 '10 at 01:51
  • 2
    @Don Stewart: It's not so much denotation as the practical end result: `IO` is better than nothing, but with sufficient perverse intent it opens you up to many of the same pitfalls and mistakes that code in an impure language would. The benefit Haskell offers is that it's easier to avoid such situations in the first place. But it doesn't sound like that's relevant to the questioner, so no worries. – C. A. McCann May 23 '10 at 02:08
  • 2
    It's not "you can write purely functional code in any language". It's "this is a piece of C code not written by me, it that purely functional?" – RnMss Mar 03 '14 at 12:50
6

30 years ago there was lispkit lisp
Not sure how accesible it is today.
[Thats one of the places where I learnt functional programming]

Anuradha
  • 121
  • 2
  • 3
  • I'm currently studying it. I've managed to compile the pascalcode and it runs. The user interface assumes that you want to reload every small program separately because it assumes you 're really short on ressources. I came here because I'm trying to understand what the claim of functional programming means. The fibonacci benchmarks starts with (letrec and I don't know how to run it in a functional fashion. – Albert van der Horst Jul 27 '20 at 17:05
6

inconsistent and non-extendable syntax

What is "inconsistency" here?

It is odd to base a language choice soley on syntax. After all, learning syntax will take a few hours -- it is a tiny fraction of the investment required.

In comparison, important considerations like speed, typing discipline, portability, breadth of libraries, documentation and community, have far greater impact on whether you can be productive.

Ignoring all the flame bait, a quick google for immutable Scheme yields some results: http://blog.plt-scheme.org/2007/11/getting-rid-of-set-car-and-set-cdr.html

Don Stewart
  • 137,316
  • 36
  • 365
  • 468
  • Why not split the difference and use Liskell? (To the OP: I edited the original post to remove the flamebait. If you don't want to have a "flamewar", keep your comments about the syntax to yourself. Otherwise, your question will be closed for being subjective and argumentative.) – jrockway May 23 '10 at 01:50
  • @nickname: As I mentioned in my answer, a lot of Haskell's syntax is actually optional fluff. Just `data`, `case`, and lambdas will let you do almost everything you'd want to do. – C. A. McCann May 23 '10 at 01:54
  • I tried to remove all of the flame bait and reference to it, including the comments explaining the syntax. I'm still new here, so I'm still unsure about the exact protocols for asking and answering questions. Thanks for your help :) – nickname May 23 '10 at 01:57
  • 1
    i'd suggest your entire question could be reduced to: "I'm interesting in playing with a purely functional / referentially transparent version of Scheme. Does anyone know of one?". – Don Stewart May 23 '10 at 01:59
  • 1
    @Don Stewart - Done. Thanks for helping me clarify the question. – nickname May 23 '10 at 02:11
5

there is owl lisp, a dialect of scheme R5RS with all data structures made immutable and some additional pure data structures. It is not a large project, but seems to be actively developed and used by a small group of people (from what I can see on the website & git repository). There are also plans to include R7RS support and some sort of type inference. So while probably not ready for production use, this might be a fun thing to play with.

Yefim Dinitz
  • 414
  • 3
  • 9
4

If you like lisp's syntax then you can actually do similar things in Haskell

let fibs = ((++) [1, 1] (zipWith (+) fibs (tail fibs)))

The let fibs = aside. You can always use s-expr syntax in Haskell expressions. This is because you can always add parentheses on the outside and it won't matter. This is the same code without redundant parentheses:

let fibs = (++) [1, 1] (zipWith (+) fibs (tail fibs))

And here it is in "typical" Haskell style:

let fibs = [1, 1] ++ zipWith (+) fibs (tail fibs)
yairchu
  • 23,680
  • 7
  • 69
  • 109
3

There are a couple of projects that aim to use haskell underneath a lispy syntax. The older, deader, and more ponderous one is "Liskell". The newer, more alive, and lighter weight one is hasp. I think you might find it worth a look.