The Artima Developer Community
Sponsored Link

Weblogs Forum
Typing like a Functional Programmer

51 replies on 4 pages. Most recent reply: Dec 8, 2006 2:16 PM by Paul Berg

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 51 replies on 4 pages [ « | 1 2 3 4 | » ]
nes

Posts: 137
Nickname: nn
Registered: Jul, 2004

Re: Typing like a Functional Programmer Posted: Nov 28, 2006 12:39 PM
Reply to this message Reply
Advertisement
> > Bingo! And that is also the reason why naive
> > implementations of funcional languages use so much
> memory.
> > They create a gazillion copies of objects where a
> > imperative language just reuses the same one.
>
> A naive implementation of any language will always be
> naive, and will have problems with memory consumption,
> incorrect behaviour, and efficiency.

[tounge firmly in check] Yes, like almost 90% of language implementations available out there ;-)

James Watson

Posts: 2024
Nickname: watson
Registered: Sep, 2005

Re: Typing like a Functional Programmer Posted: Nov 28, 2006 12:48 PM
Reply to this message Reply
> Sure. If that was not so GHC Haskell would not be nearly
> as fast as it is, that is why I said "naive".

Sorry, I missed that qualification in your statement.

Morgan Conrad

Posts: 307
Nickname: miata71
Registered: Mar, 2006

Re: Typing like a Functional Programmer Posted: Nov 28, 2006 1:16 PM
Reply to this message Reply
"In virtually every language you always need something on the left of the = sign"

DOH! To clarify, in FP, do you (almost) always need the = sign. That is, nothing returns "void", and you better save what it returns or it's gone.

In Java or C++, many methods return void (or you can ignore the return), e.g.

myList.add(something);
someResource.close();
myGuiThingie.redraw();

none of which have an equals sign nor a left side, nor would they make a lot of sense.

Alex Stojan

Posts: 95
Nickname: alexstojan
Registered: Jun, 2005

Re: Typing like a Functional Programmer Posted: Nov 28, 2006 1:21 PM
Reply to this message Reply
> > I agree. FP paradigm is useful with non-FP languages as
> > well, but what I meant was using an FP language for a
> > major project. There doesn't seem to be clear evidence
> > that those are "better" than imperative ones.
>
> That would be a subjective claim and impossible to verify.
> There is never going to be an answer to the question of
> which approach is "better".
>
> I believe it behooves a programmer to learn both
> approaches to be able to make better educated design
> decisions. Some problems just seem to be better suited to
> one approach or another.
>


That's true. However, in a large project there are lots of subproblems for which functional approach is not well suited, so you either end up using multiple languages or you use one good general-purpose language (which usually doesn't turn out to be an FP language).


> To answer your original question one well known
> industrial-strength example of successful Functional
> programming use is at Google (
> http://labs.google.com/papers/mapreduce.html ). Functional
> programming approaches are also very popular in parsers,
> compilers and interpreters.

And yet those are mostly written in an imperative language. You also wrote HeronFront in C++, not Haskell, ML or Lisp. My point here is just an observation that FP languages never made it to the mainstream software development, even after almost 50 years of R&D, although some ideas from FP are making their way into imperative languages. I think a "hybrid" language might be able to offer the best of both worlds.

nes

Posts: 137
Nickname: nn
Registered: Jul, 2004

Re: Typing like a Functional Programmer Posted: Nov 28, 2006 1:57 PM
Reply to this message Reply
> "In virtually every language you always need something on
> the left of the = sign"
>
> DOH! To clarify, in FP, do you (almost) always need the =
> sign. That is, nothing returns "void", and you better
> save what it returns or it's gone.
>
> In Java or C++, many methods return void (or you can
> ignore the return), e.g.
>
> myList.add(something);
> someResource.close();
> myGuiThingie.redraw();
>
> none of which have an equals sign nor a left side, nor
> would they make a lot of sense.

You are correct. AFAIK in Lisp there are no statements but everything is an expression; in other words everything returns a value. Even "if" or "for" are just functions that accept expressions and return something.

Christopher Diggins

Posts: 1215
Nickname: cdiggins
Registered: Feb, 2004

Re: Typing like a Functional Programmer Posted: Nov 28, 2006 2:13 PM
Reply to this message Reply
> > > Bingo! And that is also the reason why naive
> > > implementations of funcional languages use so much
> > memory.
> > > They create a gazillion copies of objects where a
> > > imperative language just reuses the same one.
> >
> > A naive implementation of any language will always be
> > naive, and will have problems with memory consumption,
> > incorrect behaviour, and efficiency.
>
> [tounge firmly in check] Yes, like almost 90% of language
> implementations available out there ;-)

Including my own :-)

Cleo Saulnier

Posts: 77
Nickname: vorlath
Registered: Dec, 2005

Re: Typing like a Functional Programmer Posted: Nov 29, 2006 4:21 AM
Reply to this message Reply
Map Reduce doesn't work in FP unless A) you have the entire list or can generate it from functions; or B) you break the functional paradigm in order to fetch values as they come in from an external source. So using Map/Reduce to promote functional programming isn't on the level IMO. If anything, Map/Reduce is a flow based technique.

Personally, I've not done that much in Scheme, but I've done more than I want to see. I never really "got" it or whatever is it that everyone else says I'm supposed to get an "aha" moment. Scheme or Lisp always gets a "Yawn!" from me. Passing around internal functions as delayed execution modules with partial input never appealed to me. I like to go all the way or not at all. Functional, with its start-the-job-but-don't-finish-right-away qualities is at odds with me. We'll leave the syntax out of it for now. Not that I don't delay things myself, but I choose to do nothing at all or finish it. Being half-ass is deceitful.

Achilleas Margaritis

Posts: 674
Nickname: achilleas
Registered: Feb, 2005

Re: Typing like a Functional Programmer Posted: Nov 29, 2006 5:35 AM
Reply to this message Reply
> And yet those are mostly written in an imperative
> language. You also wrote HeronFront in C++, not Haskell,
> ML or Lisp. My point here is just an observation that FP
> languages never made it to the mainstream software
> development, even after almost 50 years of R&D, although
> some ideas from FP are making their way into imperative
> languages. I think a "hybrid" language might be able to
> offer the best of both worlds.

Functional programming contains many useful and valuable constructs (like currying that Chris kindly enough explained) but it also contains an irritating feature called 'referential transparency'.

The benefits of functional programming come not from referential transparency but lack of state dependencies between functions. Referential transparency gets in the way.

I have written a blog in which I explain why:

http://warp9point99.blogspot.com/2006/11/sincere-comment-on-functional.html

Alex Stojan

Posts: 95
Nickname: alexstojan
Registered: Jun, 2005

Re: Typing like a Functional Programmer Posted: Nov 29, 2006 7:34 AM
Reply to this message Reply
> > And yet those are mostly written in an imperative
> > language. You also wrote HeronFront in C++, not
> Haskell,
> > ML or Lisp. My point here is just an observation that
> FP
> > languages never made it to the mainstream software
> > development, even after almost 50 years of R&D,
> although
> > some ideas from FP are making their way into imperative
> > languages. I think a "hybrid" language might be able to
> > offer the best of both worlds.
>
> Functional programming contains many useful and valuable
> constructs (like currying that Chris kindly enough
> explained) but it also contains an irritating feature
> called 'referential transparency'.
>
> The benefits of functional programming come not from
> referential transparency but lack of state dependencies
> between functions. Referential transparency gets in the
> way.
>
> I have written a blog in which I explain why:
>
> http://warp9point99.blogspot.com/2006/11/sincere-comment-on
> -functional.html

Currying is nice when you look at it in isolation, but my question is whether constructs like that offer real advantage _most_ of the time when developing large systems. It is true that different problems might require FP style, but the question is how often is that the case and is it justified to sacrifice techniques that imperative programming offers? If it's not very often then we're better off using a language like C++ that can allow us to use a limited form of FP only when we need it. For example, it's not true that most of the time everything should be a function or that most functions need to be curried or that we need to pass and return functions/lambda-expressions most of the time. I just think that FP style offers advantages in certain types of problems, so it should be used _only_ when such problems occur, rather than imposing it on programmers all the time.

Isaac Gouy

Posts: 527
Nickname: igouy
Registered: Jul, 2003

Re: Typing like a Functional Programmer Posted: Nov 29, 2006 12:36 PM
Reply to this message Reply
Alex Stojan wrote is it justified to sacrifice techniques that imperative programming offers?
Which techniques?

Alex Stojan

Posts: 95
Nickname: alexstojan
Registered: Jun, 2005

Re: Typing like a Functional Programmer Posted: Nov 29, 2006 1:09 PM
Reply to this message Reply
> Alex Stojan wrote is it justified to sacrifice
> techniques that imperative programming offers?

> Which techniques?

I meant things that we often use in languages like C++, for example:

- all OOP related techniques and principles
- generic programming
- using constructors/destructors to allocate and deallocate resources (RAII)
- using exemplars or prototype objects to serve as basis for new objects
- using function objects
- defining copy-constructors
etc.

Isaac Gouy

Posts: 527
Nickname: igouy
Registered: Jul, 2003

Re: Typing like a Functional Programmer Posted: Nov 29, 2006 3:07 PM
Reply to this message Reply
> I meant things that we often use in languages like C++

I see, you really are talking quite specifically about C++ rather than about imperative programming

> - all OOP related techniques and principles
Obviously not a requirement for imperative programming

> - using constructors/destructors to allocate and
> deallocate resources (RAII)
Obviously not a requirement for imperative programming

> - using exemplars or prototype objects to serve as basis
> for new objects
Obviously not a requirement for imperative programming

> - using function objects
Obviously not a requirement for imperative programming

> - defining copy-constructors
Obviously not a requirement for imperative programming


> - generic programming
Rather than repeat the same comment, I'll ask what makes you think that FP does not support generic programming?

Alex Stojan

Posts: 95
Nickname: alexstojan
Registered: Jun, 2005

Re: Typing like a Functional Programmer Posted: Nov 29, 2006 3:31 PM
Reply to this message Reply
> > - generic programming
> Rather than repeat the same comment, I'll ask what makes
> you think that FP does not support generic programming?

It does through dynamic typing (probably depends on the language), but my point is not whether all these things are a requirement of imperative or functional programming but does strictly using an FP language provides more benefits than strictly using an imperative one.

Isaac Gouy

Posts: 527
Nickname: igouy
Registered: Jul, 2003

Re: Typing like a Functional Programmer Posted: Nov 29, 2006 4:01 PM
Reply to this message Reply
> > > - generic programming
> > Rather than repeat the same comment, I'll ask what
> makes
> > you think that FP does not support generic programming?
>
> It does through dynamic typing (probably depends on the
> language)

Probably depends what you mean by generic programming.


> but my point is not whether all these things
> are a requirement of imperative or functional programming
> but does strictly using an FP language provides more
> benefits than strictly using an imperative one.

I don't think that's answerable as stated - it's too general. My guess is that the answer depends on what we are doing, the specific imperative and functional implmentations we are using, the skill set of the development team etc etc

Alex Stojan

Posts: 95
Nickname: alexstojan
Registered: Jun, 2005

Re: Typing like a Functional Programmer Posted: Nov 29, 2006 6:07 PM
Reply to this message Reply
> > > > - generic programming
> > > Rather than repeat the same comment, I'll ask what
> > makes
> > > you think that FP does not support generic
> programming?
> >
> > It does through dynamic typing (probably depends on the
> > language)
>
> Probably depends what you mean by generic programming.
>

I was thinking in more specific terms, for example, the following C++ function is generic:

template<class T> bool equals(T x, T y) { return x == y; }

In Scheme we would write:

(define (equals x y) (= x y))

I consider the Scheme version also generic because it will work for any two objects for which this relation is defined. It's just that the C++ function will fail at compile time if = is not defined for T, while in Scheme it will fail at runtime.
I don't know other FP languages, so I'm not sure if they support generic programming more directly.

Flat View: This topic has 51 replies on 4 pages [ « | 1  2  3  4 | » ]
Topic: Typing like a Functional Programmer Previous Topic   Next Topic Topic: My Own Personal Blog

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use