How does functional programming attack the object oriented programming area?

How does functional programming attack the object oriented programming area?

Post by gavin » Fri, 08 Feb 2008 08:10:42


If one was forced to use onyl functional programming how would one
cover the areas typically handled by oo programming?
 
 
 

How does functional programming attack the object oriented programming area?

Post by Mark Tarve » Fri, 08 Feb 2008 08:26:34


You can take a fair shot at OOP in a pure FPL by modelling classes as
0-place closures. An instance of a class is created by applying that
closure which produces an association list of attribute-value pairs.
Inheritance is handled by a class calling on its superclasses when
instantiated.

Mark

 
 
 

How does functional programming attack the object oriented programming area?

Post by Xah Le » Fri, 08 Feb 2008 10:36:49

modeling OOP using functional lang is easy. Just use global variables
for holding data. Suppose f is your function's name. And suppose you
want f to behave like a class, have methods, holds data, and be able
to create a instance of it.

In a nutshell, you do like this:

(defvar f-mydata ...) ; declare global var for class f's data

; define the class f
(setq f
(lambda ()
"class f does this and that ..."

(setq mydata1 ...) ; inner data ()
(setq mydata1 ...) ; inner data ()
...

(setq method1 (lambda ...)) ; inner function (method)

(setq method2 (lambda ...)) ; inner function (method)

...
)
)

Now, to create a object out of it, like this:

(setq g f) ; now g is a instance of f

To call methdos in f, do like this:

(g method1 arg1 arg2 ...)

--------------------------
The above assuming a non-pure functional lang, that is, the lang
allows you to set a var many times. Am not sure how OOP'd be modeled
in Haskell.

For a few thousands words article explaining this in detail, see:

What are OOP's Jargons and Complexities
http://www.yqcomputer.com/

Xah
XXXX@XXXXX.COM
http://www.yqcomputer.com/



On Feb 6, 3:26 pm, Mark Tarv<r < XXXX@XXXXX.COM >uk> wrote>
> On 6 Feb, 23:10, gavi<o wrote>
> You can take a fair shot at OOP in a pure FPL by modelling classes a>
> 0-place closures. An instance of a class is created by applying tha>
> closure which produces an association list of attribute-value pairs>
> Inheritance is handled by a class calling on its superclasses whe>
> instantiated>
> Mark
 
 
 

How does functional programming attack the object oriented programming area?

Post by Pascal Cos » Fri, 08 Feb 2008 16:47:39


http://www.yqcomputer.com/ ~jar/pubs/oopis.ps
http://www.yqcomputer.com/ ~normark/scheme-oop/scheme-oop.ps

Pascal

--
1st European Lisp Symposium (ELS'08)
http://www.yqcomputer.com/ ~pcostanza/els08/

My website: http://www.yqcomputer.com/
Common Lisp Document Repository: http://www.yqcomputer.com/
Closer to MOP & ContextL: http://www.yqcomputer.com/
 
 
 

How does functional programming attack the object oriented programming area?

Post by Mark Tarve » Fri, 08 Feb 2008 17:45:35


Yes; thats something like the way I would do it.

Mark
 
 
 

How does functional programming attack the object oriented programming area?

Post by Joshua Tay » Sat, 09 Feb 2008 01:17:38


I read an interesting post about generalized folds on recursive types
recently. One exercise covered is implementing `lists' as closures. A
fun read, and not too long.

http://www.yqcomputer.com/

//J
 
 
 

How does functional programming attack the object oriented programming area?

Post by gavin » Sat, 09 Feb 2008 05:46:17


wow
scheme really kicks ass
 
 
 

How does functional programming attack the object oriented programming area?

Post by Kaz Kylhek » Sat, 09 Feb 2008 06:17:43


Hi Gavino,

One way would be to use object-oriented functional programming:
immutable objects only. No mutator methods, just accessors and
constructors. Functional and OO programming are not mutually exclusive
opposites; object-orientation doesn't imply the use of imperative
programming.

You can simulate an object changing state by constructing another one
like it, but with different contents analogous to what the state
change would be. Constructors (like the CONS function in Lisp) are
functional. Object accessors (like CAR and CDR in Lisp) are also
functional.

OO programming was developed to support simulations, and that makes a
good example.

A simulation under functional programming, might look like the
repeated application of a filter function ``next-state'' to a dynamic
set of objects representing the simulated universe. This next-state,
applied to the universe U at time N would return a new database
representing the universe at time N+1. The new database would contain
some old objects (representing things that didn't change state) as
well as some new ones.

Imagine how much easier debugging might be if you can hang on to the
entire old version of the full state of the simulation and see what
differences next-state computed.

Of course, the simulation calls next-state in a loop, which involves
hanging on to the dynamic set in some variable which is overwritten at
each iteration with the new dynamic set.

(loop
(setf u (next-state u)))


That bit of imperative programming can be eliminated, if desired, by
using tail recursion.

(defun simulation-loop (u)
(simulation-loop (next-state u))

Of course, a real piece of simulation sofware has to produce some kind
of output: the state of the simulation after a given number of steps,
or even output from each stage (logged as a text stream, or possibly
with a dynamically updated GUI representation, etc).
 
 
 

How does functional programming attack the object oriented programming area?

Post by gavin » Sat, 09 Feb 2008 06:59:30


could you build a object oriented application server from that?
 
 
 

How does functional programming attack the object oriented programming area?

Post by gavin » Sat, 09 Feb 2008 07:00:48


can you use that to create an object oreiented application server?
 
 
 

How does functional programming attack the object oriented programming area?

Post by Christophe » Sat, 09 Feb 2008 15:28:15


Yeah, Scheme's designed to kick ass easily, since you'll need to do it
often.
Now, Common Lisp has most of the ass-kickin' built in, allowing you to
behave more socially.
 
 
 

How does functional programming attack the object oriented programming area?

Post by Christophe » Sat, 09 Feb 2008 15:31:25


Can YOU use anything to create any kind of application?

No, don't answer, this is a rhetorical question!
Oh, usenet, why do I bother?