views:

436

answers:

5

Hi, I often find developers use the terms functional language and dynamic language together, and wonder why are they always being put together. What are the differences between them? Can a language be both dynamic and functional? Do they complement each other? Why do we need them anyway? I'm a C# programmer and don't yet understand this whole dynamic/functional thing (C# is going to have some dynamic features in ver 4. Will it also be functional? what's going on here?).

Thanks, Abraham

+6  A: 

Dynamic typing, a type system, is orthogonal to 'functional', a programming paradigm.

Dynamic 'languages' are actually dynamically typed. This means that you don't have compile-time checking of your variable types.

Functional languages offer loads of support for e.g. lambda calculus - anonymous functions.

An example of a language that does dynamic typing, and supports anonymous functions: javascript. Ruby has some functional style support, too. And there are others.

xtofl
No mention of Lisp?
Nikhil Chelliah
Not all dynamic languages have to dynamically typed. Don't mix up vocabulary
jitter
@jitter: It depends on what you mean by "dynamic language" really. I think it's a somewhat ambiguous term.
Jon Skeet
@Nikhil: feel free to add any functional language you know of :) Personally I have little experience with Lisp - yet.
xtofl
+6  A: 

To put it in a simple (but not accurate) answer

  • Dynamic languages are ones in which the Type (Name of the Class) is not as important as compared to its nemesis statically typed languages. A variable may have objects of different types assigned to it at any given point of time. Method invocations are resolved at run-time. This means you lose the benefits of static typing (compiler warnings) but simple methods turn generic - sort(list) works for a list of strings as well as list of ints. e.g. Ruby et. all
  • Functional languages value immutability. The programs are written in terms of bigger and bigger functions (usually bottom up). The concept of object state and mutability is frowned upon. A function in this context is self-sufficient (The term is Pure as per Wikipedia): everything it needs to produce output, lies in the input that it receives. It also produces no side-effects (unless it explicitly mentions it) and returns consistent output for a given input. This can lead to elegant code (see: fluent interfaces), where input data is pipelined through diff functions to produce the eventual output e.g. LISP et.all

However the boundaries are being muddied with languages picking up the best of all worlds... You could have a language which is both, one or neither.
e.g. predominantly static C# picking up lambda expressions in 3.0 and bringing in dynamic capabilities with 4.0

Gishu
If functional languages are composed of functions, aren't they a step back, to the days of procedural languages? How can large scale applications be built only from functions?
A BIG problem with procedural languages was global state and everyone trampling over everyone else, 'functions' in functional languages are "Pure" as in they don't access any state/data other than their input. Wikip does a good job with the 'Pure functions' section. However good point.. I'll clarify my post
Gishu
Functional languages could be considered the opposite of procedural languages. Most popular languages these days are essentially procedural with some OO features. Functional languages are something very different, more like math and less like machine instructions.
Chuck
+2  A: 

xtofl has already offered a good overall picture. I can speak to the C# point.

C# has been becoming easier to work with in a functional way for a while now:

  • C# 2 introduced anonymous methods, which made it easier to create delegates which used state which was otherwise local to a method
  • C# 3 introduced lambda expressions which are mostly like anonymous methods but even more compact
  • LINQ support in both C# 3 and .NET 3.5 made it easier to query data in a functional way, chaining together predicates, projections etc
  • None of the C# 4 features directly contributes to functional programming IMO, although named arguments and optional parameters may make it easier to create/use immutable types, which is one of the biggest features missing from the functional picture IMO.

(There are other things functional languages often have, such as pattern matching and more impressive type inference, but you can write a lot of functional-style code reasonably easily in C#.)

C# 4 will gain some dynamic abilities through the dynamic type (which itself is effectively a static type you can do anything with). This will be somewhat "opt in" - if you never use the dynamic type, C# will still be fully static language. There's no language support for responding dynamically, but the DLR has support for this - if you implement IDynamicMetaObjectProvider or derive from DynamicObject, for example, you can add dynamic behaviour.

I would say that C# isn't becoming a functional language or a dynamic language, but one in which you can code in a functional style and interoperate with dynamic platforms.

Jon Skeet
Well, is kind of sounds as if the dynamic part of language is the concern of the compiler or its execution environment, while the functional part is the concern of the developer, as he as to program in a certain way, a functional way (although not quite, as to me it looks like a dynamic language lets you program in a generic way, which is also a paradigm I guess).BTW, it sounds a little funny calling a language a 'Functional' language. If a language is not functional, is it not a good language, i.e not 'functional'? Not pragmatic :)
I think developers understand that "functional" is an overloaded term :) I guess today it might be called "function-oriented" - blech!
Jon Skeet
+2  A: 

If you're interested in paradigms, the paper Programming Paradigms for Dummies: What Every Programmer Should Know covers them.

In functional programming, state is implicit - the program executes by calling functions which call other functions. In imperative programming and object oriented programming, state is explicit - you change the value of a variable or object's field.

In a way, functional and imperative systems can be seen as duals - what's fixed in one is a dynamic value in the other.

Closures - which trap some explicit, mutable state in an object which can be called as a function - sit somewhere between, being neither pure functional programming but not quite fully fledged objects; they are more like anonymous objects than functions.

'Dynamic languages' is vague term, usually meaning one of the following:

  • Dynamically Typed Languages - languages which delay determination of type to runtime, but the set of types is fixed. Examples are Smalltalk, Lisps, current Fortress implementations. Some otherwise statically typed languages also allow some dynamic type checks - Java, C#, C++ and Ada. ( it was a failed dynamic type cast from float to int in Ada that crashed Ariane 5 )

  • Languages with dynamic types - languages where new types can be created at runtime. The most popular is JavaScript. Because you have to run the program to determine the types, it's harder to make IDEs for these with type aware autocompletion.

  • Languages which are dynamically compiled - languages where new scripts can be compiled at runtime. This is true of bash, JSP, PHP and ASP at the page scale, and true for at a finer scale for lisps and JavaScript which support an 'eval' function which compiles and runs an expression.

Functional languages which are strongly typed often perform a large amount of type inference, so it's common for their programs to have less explicit typing than poorly implemented static typed languages. This can confuse people who have only seen the lack of explicit typing in dynamic typed languages into believing that type inference is the same as dynamic typing.

Pete Kirkham
+2  A: 

Dynamic typing and functional programming are independent concepts. You can have either, neither or both in a language.

Static typing means that types of objects are known at compilation time. In dynamic typing they are known at runtime.

Functional programming means programming style where computation is done by evaluating functions while avoiding state changes. (example: you use recursion instead of for-loops, because a loop would need changing of a counter variable, etc.) This helps to avoid bugs and makes concurrent programming easier. Pure languages require you to program in functional style, others just enable it.

Example languages:

|----------------+---------+---------|
|                | Dynamic | Static  |
|----------------+---------+---------|
| Functional     | LISP    | Haskell |
| Not functional | PHP     | Java    |
|----------------+---------+---------|

Dynamic languages on the other hand are a broader concept. There is no exact definition, but usually the more features of the compiler are moved to the runtime, more dynamic the language is. This means that in dynamic languages you can usually evaluate expressions, change object structure etc. at runtime.

abababa22