What languages can be used as a functional language

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

zenten
Posts: 3799
Joined: Fri Jun 22, 2007 7:42 am UTC
Location: Ottawa, Canada

What languages can be used as a functional language

Postby zenten » Thu Apr 10, 2008 9:04 pm UTC

Ok, so there are functional languages out there, that call themselves functional languages and all that. I'm not talking about them.

What languages can be used entirely as functional languages? I know Python is one. Apparently C++ can use lambdas if you use some additional libraries, as can Java. Is there anything more they would need?

So what languages can and can not be used in a purely functional manner?

User avatar
Xanthir
My HERO!!!
Posts: 5281
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: What languages can be used as a functional language

Postby Xanthir » Thu Apr 10, 2008 9:55 pm UTC

Haskell, obviously. Lisp, obviously. Both of these were built as functional languages and had other paradigms built in later (Haskell in the form of monads, Lisp in the form of goddamn everything).

I'm not very familiar with OCaml, but it says it supports functional programming.

zenten wrote:Ok, so there are functional languages out there, that call themselves functional languages and all that. I'm not talking about them.

I'm slightly confused about what languages would fit in this category. Do you mean that you want languages which can be used functionally, but aren't built explicitly to be functional?
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

zenten
Posts: 3799
Joined: Fri Jun 22, 2007 7:42 am UTC
Location: Ottawa, Canada

Re: What languages can be used as a functional language

Postby zenten » Fri Apr 11, 2008 3:47 am UTC

Xanthir wrote:
zenten wrote:Ok, so there are functional languages out there, that call themselves functional languages and all that. I'm not talking about them.

I'm slightly confused about what languages would fit in this category. Do you mean that you want languages which can be used functionally, but aren't built explicitly to be functional?


Well, Lisp is one, and Haskell is another ;)

I'm asking about languages like Python, that aren't designed to be Functional, but can be if you only use a subset of the language.

coppro
Posts: 117
Joined: Mon Feb 04, 2008 6:04 am UTC

Re: What languages can be used as a functional language

Postby coppro » Fri Apr 11, 2008 4:07 am UTC

C++ templates :D

User avatar
Sc4Freak
Posts: 673
Joined: Thu Jul 12, 2007 4:50 am UTC
Location: Redmond, Washington

Re: What languages can be used as a functional language

Postby Sc4Freak » Fri Apr 11, 2008 7:05 am UTC

Yes, C++ is often described as a "multi-paradigm" language. That means that you can do almost whatever you want with it. Functional programming is no exception. Combining functors and templates (eg. std::sort) allows to you essentially have a function that's a first class object.

User avatar
froggie
Posts: 23
Joined: Wed Jul 18, 2007 6:50 pm UTC

Re: What languages can be used as a functional language

Postby froggie » Sat Apr 12, 2008 8:17 am UTC

Javascript, Erlang, Ruby... :D

btilly
Posts: 1877
Joined: Tue Nov 06, 2007 7:08 pm UTC

Re: What languages can be used as a functional language

Postby btilly » Sat Apr 12, 2008 8:57 am UTC

Add Perl to the list. And I highly recomment Higher Order Programming to anyone who is interested to learn how to use some functional techniques in that language.

Note that Python's functional support is actually fairly weak. You don't have multi-line lambdas, which is a huge restriction. That won't be improved in Python 3, which for a while was not going to have lambda at all. (It still won't include reduce.) The root cause of this deficiency is that Guido van Rossum does not like closures or functional programming very much. (I have seen him say this both in public and in private enough times to believe him.)

Also one correction.
Xanthir wrote:Haskell, obviously. Lisp, obviously. Both of these were built as functional languages and had other paradigms built in later (Haskell in the form of monads, Lisp in the form of goddamn everything).

You're wrong about Lisp. Lisp was not designed as a functional language. In fact it would have been hard to have done so - when Lisp was designed in the 50s the idea of functional languages did not exist! But I think it is fair to say that the fundamental philosophy of functional programming first arose within the Lisp family. And Scheme was probably the first "functional language".

Incidentally the fundamental idea of a closure came, of all places, from ALGOL! Pascal had closures before any dialect of Scheme! (Of course Pascal had the notion without the notion of a first class function, which made it substantially less useful.)

The first dialect of Lisp with lexical scoping and hence closures in it was Scheme. From there other dialects quickly picked up lexical scoping with the notable exception of Emacs Lisp.
Some of us exist to find out what can and can't be done.

Others exist to hold the beer.

User avatar
evilbeanfiend
Posts: 2650
Joined: Tue Mar 13, 2007 7:05 am UTC
Location: the old world

Re: What languages can be used as a functional language

Postby evilbeanfiend » Sat Apr 12, 2008 9:46 am UTC

tcl can certainly be written in a functional style (its a polish notation language so it can look pretty much like lisp but with [] instead of () its also is very good at treating data as code like lisp.). like other languages you need to fake continuations and lambdas if you want them (the lambdas is certainly very easy to fake)

tcl, python and even emacs lisp, while undeniable can be used to write in a functional style miss out on some important features such as tail call optimization which practically limit what you can do in a functional style with them (you can use some little helper functions to manage the stack for you but its not as natural as true TCO or of course you can change to an iterative algo.
in ur beanz makin u eveel

demon
Posts: 170
Joined: Tue Feb 06, 2007 8:13 pm UTC

Re: What languages can be used as a functional language

Postby demon » Sat Apr 12, 2008 1:33 pm UTC

OCaml is a quite powerful functional language - our functional programming course was actually taught in OCaml. The only thing it lacks is lazy evaluation. It's possible to have it, but it's not default. It also has a beautiful type system and a very cool module system. The syntax requires some adjustment, though, but after a while it starts to make sense:D

Haskell I barely know, but it has blazingly fast mathematical libraries and lazy evaluation by default - awesomeness.

Scheme is beatiful, it barely has any syntax and is still one of the most expressive languages I've ever seen. Also - SICP is online for free - what more can you ask for?:)

Python is sweet because of the intuitive syntax and the instant gratification it offers. One-line lambdas may be a pain, as is the lack of the classic 'let' statement, though.

User avatar
Berengal
Superabacus Mystic of the First Rank
Posts: 2707
Joined: Thu May 24, 2007 5:51 am UTC
Location: Bergen, Norway
Contact:

Re: What languages can be used as a functional language

Postby Berengal » Sat Apr 12, 2008 2:00 pm UTC

Using the new <expr> if <cond> else <expr> construct in python allows for multi-line lambdas. It's not something I'd recommend doing though.

Code: Select all

factorial:
factorial = lambda n:\
                         1 if n == 1\
                         else n * factorial (n-1)

Functions with side-effects need to return either True or False, not both (most functions with side-effects return None, which is lucky):
def prt(string):
  print string
  return None #usually implicit
def log(string):
  open('logfile', 'a').write(string)
  return None

printLogAndEcho = lambda string:
                    None if prt(string)\
                    else None if log(string)\
                    else string
It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.

User avatar
Xanthir
My HERO!!!
Posts: 5281
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: What languages can be used as a functional language

Postby Xanthir » Mon Apr 14, 2008 2:05 pm UTC

btilly wrote:
Xanthir wrote:Haskell, obviously. Lisp, obviously. Both of these were built as functional languages and had other paradigms built in later (Haskell in the form of monads, Lisp in the form of goddamn everything).


You're wrong about Lisp. Lisp was not designed as a functional language. In fact it would have been hard to have done so - when Lisp was designed in the 50s the idea of functional languages did not exist! But I think it is fair to say that the fundamental philosophy of functional programming first arose within the Lisp family. And Scheme was probably the first "functional language".

While perhaps technically true, specific elements that Lisp embraced in the language design (first-class functions and expression-based, rather than statement-based, programming) make it a functional language, no matter what McCarthy originally intended for it. ^_^
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))

EvanED
Posts: 4330
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: What languages can be used as a functional language

Postby EvanED » Mon Apr 14, 2008 4:29 pm UTC

Xanthir wrote:While perhaps technically true, specific elements that Lisp embraced in the language design (first-class functions and expression-based, rather than statement-based, programming) make it a functional language, no matter what McCarthy originally intended for it. ^_^

Yes, but it was also not made to either disallow or actively discourage programming with side effects. setf exists and, at least in Common Lisp, is actually very nice. It doesn't require anything special to use (in contrast to, say, ML, where you have to declare a setable reference explicitly).

darren
Posts: 188
Joined: Fri Feb 08, 2008 5:37 pm UTC

Re: What languages can be used as a functional language

Postby darren » Mon Apr 14, 2008 8:44 pm UTC

Any language with first class function objects (functions are objects that can be passed as values to and returned from functions) and lexical closures can potentially be used as a functional language. (You might not even lexical closures, but that would be painful). Bonus points for tail recursion support (which is more of a compiler issue, not a language issue).

poohat
Posts: 230
Joined: Mon Apr 07, 2008 6:21 am UTC

Re: What languages can be used as a functional language

Postby poohat » Wed Apr 16, 2008 7:00 pm UTC

I think the better question is 'what reason is there for using something other than lisp?'

But you can do functional programming in most languages, the only question is to what extent the language ends up fighting against you rather than helping you. You can do complex stuff involving closures/currying/whatever in Java, but it isnt going to be a very pleasant experience compared to doing it in lisp/ocaml.

EvanED
Posts: 4330
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: What languages can be used as a functional language

Postby EvanED » Wed Apr 16, 2008 7:10 pm UTC

poohat wrote:I think the better question is 'what reason is there for using something other than lisp?'

Some people like [statically] typed languages.

Tac-Tics
Posts: 536
Joined: Thu Sep 13, 2007 7:58 pm UTC

Re: What languages can be used as a functional language

Postby Tac-Tics » Wed Apr 16, 2008 7:12 pm UTC

zenten wrote:So what languages can and can not be used in a purely functional manner?


It really depends on what you mean by "purely functional" (and to a lesser extent what you mean by "use"). If you're interested in languages which minimize the use of side-effects, any language supporting nestable functions will do. Things like GC and closures sure help though.



btilly wrote:Also one correction.
Xanthir wrote:Haskell, obviously. Lisp, obviously. Both of these were built as functional languages and had other paradigms built in later (Haskell in the form of monads, Lisp in the form of goddamn everything).

You're wrong about Lisp. Lisp was not designed as a functional language. In fact it would have been hard to have done so - when Lisp was designed in the 50s the idea of functional languages did not exist! But I think it is fair to say that the fundamental philosophy of functional programming first arose within the Lisp family. And Scheme was probably the first "functional language".


According to this one crazy guy named McCarthy, the original LISP was indeed purely functional. Just check out eval. Not at set! in sight! No IO primitives either. Like Miranda and other early pure languages, the only interaction with your program is running your program in a REPL, typing in the expressions you want to run, and then having the REPL display the valuations of those expressions.

However, when I first learned about Common Lisp, the damn language really threw me off! My journey into functional languages looked something like

Paul Graham's CL implementation of McCarthy's Lisp (linked above) => Scheme => Haskell => Common Lisp

Common Lisp just barely passes for functional. The design of the language has a very imperative feel. It demonstrates very clearly the language's age and focus on practicality over purity. Side effects are everywhere in the language!

My point is don't confuse Lisp (the family) with Common Lisp. The Lisp family is absolutely enormous, and because of that fact, you can call it functional or imperative and either way you're right!
Last edited by EvanED on Wed Apr 16, 2008 7:16 pm UTC, edited 1 time in total.
Reason: Fixed URL tag; don't use quotes

User avatar
jspenguin
Posts: 84
Joined: Wed Apr 16, 2008 7:39 pm UTC
Contact:

Re: What languages can be used as a functional language

Postby jspenguin » Wed Apr 16, 2008 8:58 pm UTC

Lua is an *excellent* language with first-class functions (including anonymous functions), full lexical scoping, tail recursion, and incremental GC (no GC pause). It was designed from the start to be portable and embeddable -- It is the most commonly used language for actor scripting in games.

Wikipedia

I use Python most of the time, but the next time I need to combine something with C, I will use Lua.

Code: Select all

from __future__ import skynet

qbg
Posts: 586
Joined: Tue Dec 18, 2007 3:37 pm UTC

Re: What languages can be used as a functional language

Postby qbg » Thu Apr 17, 2008 1:46 am UTC

EvanED wrote:
poohat wrote:I think the better question is 'what reason is there for using something other than lisp?'

Some people like [statically] typed languages.

Hmm...

User avatar
Mortabis
Posts: 50
Joined: Fri Feb 08, 2008 12:24 am UTC

Re: What languages can be used as a functional language

Postby Mortabis » Fri Apr 18, 2008 11:31 pm UTC

If you want to do functional stuff, I wouldn't suggest using C++. It'll work but as far as I can tell from my own experience programming functional stuff is particularly simplified when you have higher-level features like garbage collection available. All paradigms are usually made easier, in a sense, by programming in a higher level language, but functional imo is easier by a far greater degree. Most stuff I've seen that uses functional programming doesn't really need to run fast, and doesn't really benefit from low-level control.

But don't give my opinion undue weight. I actually haven't done very much with functional programming (this does not translate to none), and there are many more experienced programmers and computer scientists on these fora than me. They probably have better advice.

Also I agree with jspenguin about Lua, it's a great language that often gets overlooked. You can code sections of Lua programs in C as well, much like including ASM code in C. I'm pretty sure that you can actually put ASM into Lua in this manner, but I haven't tested. It's also rather fast. A lot of games use it for their scripting stuff as a result, e.g. Crysis.
Image


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 6 guests