functional programming is nifty!

Status
Not open for further replies.

dockeen

Ars Tribunus Militum
1,600
OK, let me be the straight man and let y'all educate this dumb
ol' country boy.

What is the top level, here's the dog, here's the tree
explanation of functional programming languages versus
what I'm used to...Pascal, Ada, C, Perl, Python etc.?

What are the strengths of functional languages, i.e. what
sort of problems are they well suited for, not so well
suited for?

Thanks,
Wayne (ibahick)
 
D

Deleted member 5324

Guest
Originally posted by dockeen:
What is the top level, here's the dog, here's the tree
explanation of functional programming languages versus
what I'm used to...Pascal, Ada, C, Perl, Python etc.?

These specific ones we've mentioned, Haskell and O'Caml (which is just a representative from the group of impure functional languages known as ML) are both strictly typed. That means that if there's a possible type error in your code, it won't compile. It also means that if you code compiles and doesn't run, you've got an algorithmic problem somewhere -- the compilers are so very strictly that it couldn't be otherwise.

That's the biggest difference you'll notice between a language like O'Caml and a language like Python or Perl. I've done Python and Perl, and they're great languages, providing many of the features (but not nearly the speed) that O'Caml provides. But I still use O'Caml, because it catches so many of my errors that I can actually write large and useful programs with it. A better programmer might not need the strictness of the O'Caml compiler, but me, I really do make use of it.

As far as languages like C or Pascal go, these functional languages (O'Caml and Haskell, among others) provide higher levels of abstraction, due to the ability not only to use functions as data, but also because they provide the ability to easily and efficiently create new types. O'Caml (I don't know about Haskell) also provides an object system with inheritance and encapsulation; both O'Caml and Haskell offer module systems that support data/implementation hiding. Writing a large system with several programmers is undoubtedly more facilitated by the features of O'Caml or Haskell than by C or Pascal, whose facilities in such regards are comparatively primitive.

O'Caml offers the genericity of C++'s templates via either functorized modules or classes. Haskell's abilities in those regards I don't know of for sure, but both support parametric polymorphism (whatever that term means exactly :p)

What are the strengths of functional languages, i.e. what
sort of problems are they well suited for, not so well
suited for?

They're suitable for really every kind of problem, especially those that take input and return output :D

As far as what they're unsuitable for -- if you thought about doing it in Perl, Python, Ada, Pascal, or even in some situations C++, and said to yourself, "No, I better do this in C and/or assembler." for whatever reason (speed, efficiency, access to OS/hardware,) O'Caml or Haskell are likely ruled out for the same reasons, with the possible exception of speed -- you can generally expect O'Caml code to perform at least as well as similar C++ code would when using C++ features -- that is, slightly slower than C code. There are certain circumstances, however, where O'Caml is even faster than C, so if speed is your ultimate concern, then testing would be in order.

Does that cover it? :)

Jeremy
 
> As far as languages like C or Pascal go, these functional languages
> (O'Caml and Haskell, among others) provide higher levels of
> abstraction, due to the ability not only to use functions as data,
> but also because they provide the ability to easily and efficiently
> create new types.

STRUCT = STRange acronym for User-defined abtraCt data Type
Why have more ways to make new types when you can have one?

Functional programming is all fine and dandy, and allows you to express some concepts cleanly. But I am still not convinced that it's all that and a bag of chips.
--
Stephan T. Lavavej
http://stl.caltech.edu
 
Oh, and I then noticed:

> They're suitable for really every kind of problem, especially those
> that take input and return output

No way. Functional programming languages are (in my experience - thus far Scheme and Lisp) allergic to bit-level twiddling. For example, writing a program that returns the SHA-1 cryptographically secure one-way hash of a file would probably be nasty or ugly (that's an inclusive or, by the way). In C, it's very natural and easy. And by bit-level twiddling, I do not mean unportable, assembly-like twiddling of registers, nor anything that is implementation-dependent.
 
D

Deleted member 5324

Guest
Originally posted by Stephan T. Lavavej:
STRUCT = _STR_ange acronym for _U_ser-defined abtra_C_t data _T_ype
Why have more ways to make new types when you can have one?

Do you seriously think that one is sufficient?

Did you forget that even C includes union?

Structs (without unions) aren't capable of representing sum types without making every object of the type include all possible members of that type. For that, you'll need tagged unions, which are not nearly as easy to work with as user defined types in ML. In addition, you can't do typechecked polymorphism in C -- if you want polymorphism, you're stuck with void *, which makes no guarantees whatsoever about its contents.

Making new types in O'Caml is incomparably easier, safer, and less error-prone than in C. Easier, because the syntax supports it succintly and easily (see my thread about interpreters for an example of a type declaration.) Safer, because polymorphism can be type-checked -- it's impossible in C to write a polymorphic list that allows only only a single type in it, enforced at compile time; the best you can do is a list of nodes which contain void *, but that can't be typechecked. And less error prone simply because far less code has to be written to create an abstract data structure in O'Caml than in C.

No way. Functional programming languages are (in my experience - thus far Scheme and Lisp) allergic to bit-level twiddling. For example, writing a program that returns the SHA-1 cryptographically secure one-way hash of a file would probably be nasty or ugly (that's an inclusive or, by the way). In C, it's very natural and easy. And by bit-level twiddling, I do not mean unportable, assembly-like twiddling of registers, nor anything that is implementation-dependent.

It's not that horrible -- I've read implementations of strong cryptographic ciphers in O'Caml and they're not horrible. I won't deny that C's better than the functional languages I know at bit-twiddling, but when working with individual bits, you're generally in a situation where you also want optimal speed, so you'd be using C anyway. Few people would bother writing any serious cryptographic algorithms in Perl or Python (I don't know how well Ada does bit twiddling.)

Scheme and LISP are in an entirely different category than O'Caml or Haskell (And really, Haskell is in an entirely different category than O'Caml, but O'Caml is closer than Scheme is.) Scheme and LISP are both dynamically typed, whereas O'Caml and Haskell are both statically typed (and Haskell is lazily evaluated, which is why it's different than all the others entirely, which are strictly evaluated.)

Functional programming is all fine and dandy, and allows you to express some concepts cleanly. But I am still not convinced that it's all that and a bag of chips.

You also don't have much experience with it. I'm not saying you'll be a convert the second you learn O'Caml, but you're really rather too inexperienced with entire classes of functional languages to pass judgment on their utility as a whole.

Jeremy
 
Actually, unions are seen very rarely in comparison to structs. I believe the proper reference for this is _Expert C Programming: Deep C Secrets_ by Peter van der Linden (or perhaps _C Traps and Pitfalls_ by Andrew Koenig), but I don't have either with me right now. I could only bring one C programming book home with me, and it had to be a K&R2.

You can harp on my lack of knowledge wrt functional programming - in fact, I'm probably going to gut this essay in light of it - but I know a fair amount about C, and more every day. ^_^
--
Stephan T. Lavavej
http://stl.caltech.edu
 
D

Deleted member 5324

Guest
Originally posted by Stephan T. Lavavej:
Actually, unions are seen very rarely in comparison to structs.

But that doesn't make them rare, as stickboy mentioned.

I believe the proper reference for this is _Expert C Programming: Deep C Secrets_ by Peter van der Linden (or perhaps _C Traps and Pitfalls_ by Andrew Koenig), but I don't have either with me right now. I could only bring one C programming book home with me, and it had to be a K&R2.

You can quote all the books you want, but that doesn't make it so that the all the C code I've been looking at in recent times has made use of unions. As stickboy mentioned, it's used quite a bit in certain situations, one of which is the development of other dynamically typed languages.

You can harp on my lack of knowledge wrt functional programming - in fact, I'm probably going to gut http://stl.caltech.edu/essay3.html in light of it

If by "gut" you mean "remove all the hideous misinformation about functional programming," then I suppose that's a good thing :)

(And also, as a note, C isn't "allergic garbage collecting" -- you simply plug in a garbage collector that provides its own malloc and use that.)

- but I know a fair amount about C, and more every day. ^_^

I know my fair share of C too. But I also know functional programming languages -- an area in which it seems you'd do better to research before you make sweeping claims and broad generalizations about it.

Jeremy
 

.mobius

Ars Scholae Palatinae
1,139
I think a lot of it is where you start. I began with BASIC and then moved to VB and a little of C++, but at the beginning of the year, I tried to start over completely with Structure and Interpretation of Computer Programs, which uses Scheme. Now, I'm really enjoying using a functional programming language, and when I sit down and try to do something in C++ (or more recently, Python), I end up thinking "Why is this so hard? I could do this so much more quickly in Scheme."

Now, C (and C++, by extension) is a handy language, and it's hard to beat for a down-and-dirty language to interact with hardware and to do imperative tasks--I used it exclusively while working on robotics this summer (not entirely true; I did use the Lego MindStorm language for a little bit, until I found a C compiler for it :D ), but now that functional programming has "clicked" with me, it feels much more powerful and graceful than imperative languages.

It doesn't happen overnight, and you can't just pick it up and start programming: unlike C++ or Python, where I've found that I can just start working and learn how to do things as I come to them, functional programming needs a good bit of forethought and understanding of the problem and the method being used to solve it.

So anyways, that's my little comment on the subject. And to answer the original poster: Yes, yes it is.


(.mobius)

PS. Ooh. I just had a thought. I may have to go get the O'Caml stuff, and see if I can use it to do what I'm doing in Python right now. And I've heard there's a scheme shell for Linux. That could be a lot of fun.
 
D

Deleted member 5324

Guest
Originally posted by .mobius:
PS. Ooh. I just had a thought. I may have to go get the O'Caml stuff, and see if I can use it to do what I'm doing in Python right now. And I've heard there's a scheme shell for Linux. That could be a lot of fun.

I generally very strongly prefer one language at a time (much as a person with more than one car still much prefers his pickup truck over his minivan or his sedan when the situation doesn't call for any car in particular.) That language used to be Python, but now (and for the past 6 months or so) it's O'Caml.

The main reason I switched was because in reality, I'm a rather poor programmer. The dynamic nature of Python means the compiler can't check much of anything at all before running my code, so once my code for my IRC bot got "big" (that is, once it hit 2k lines of code :D) I would have so many latent bugs in sections of code that wasn't run often that I really couldn't trust my bot to stay alive. I'd find one bug, run the program, and a few minutes later another bug I had no idea about would raise some exception, generally a TypeError or an AttributeError, often caused by a typo, often caused by a braino. Either way, that's why I switched: I can trust O'Caml programs to run far more correctly than I can anything I've written in Python.

One of the first things I did in O'Caml was translate the Python modules I really needed to do my IRC bot in O'Caml. I've got string, fnmatch, glob, pieces of os, the tcp portion of socket (although, in reality, that wasn't needed -- it was really just to learn objects in O'Caml,) asyncore, asynchat, and pieces of time. As I get better with O'Caml, I find myself using these libraries less and less, and will probably at some point rip the code I use out and put it in its own library, and stop sticking to "python compatibility" as I have in the past.

Anyway, after translating what I needed into O'Caml, I wrote my IRC bot. It's currently at more than 10k lines of code, and it still runs rather well -- individual features might have bugs, but nothing I've yet found will crash it. Not only is it far faster (computationally) in O'Caml than in was in Python, but it's far faster to bugfix, far faster to extend, and far faster to refactor; basically, in the end (that is, after all debugging and such has been taken into account,) I'm inordinately more productive using O'Caml than I was using Python, and the quality of my code has increased significantly. For all but the simplest of system administration scripting, O'Caml is currently my language of choice.

So what are you doing in Python that you'd consider doing in O'Caml? :D

(As far as scsh (scheme shell) goes, if I need a real programming language for replacing some shell scripting, I'll use Python -- Scheme is really overkill for small shell scripting tasks, and Python's more readable syntax, more os-independent os binding, and larger user community for support ensures that the final result will almost invariably be superior when written in Python than when written in scsh.)

Jeremy
 

stickboy

Ars Tribunus Angusticlavius
6,218
Subscriptor
Originally posted by .mobius:
And I've heard there's a scheme shell for Linux. That could be a lot of fun.

I love Scheme and have tried Scheme shell briefly, but I don't think it's good as an interactive shell. (For scripting it might be better, though.) I don't mind the parentheses when coding, but for interactive use they become a bit tiresome.
 
D

Deleted member 5324

Guest
Originally posted by PeterB:
But that's not to say it hasn't ever got stuck in an infinite loop and then fallen off the server. A crash is as good as a hang in my book.

Given the intractability of the halting problem, O'Caml does about as well as is possible.

Sure, an infinite loop is just as good as a crash, but there's nothing a language can do statically to prevent those, and I don't put them into production all that often :)

Jeremy
 

.mobius

Ars Scholae Palatinae
1,139
Originally posted by jemfinch:
Quote: "Originally posted by .mobius:
PS. Ooh. I just had a thought. I may have to go get the O'Caml stuff, and see if I can use it to do what I'm doing in Python right now. And I've heard there's a scheme shell for Linux. That could be a lot of fun."

...

So what are you doing in Python that you'd consider doing in O'Caml? :D

(As far as scsh (scheme shell) goes, if I need a _real_ programming language for replacing some shell scripting, I'll use Python -- Scheme is really overkill for small shell scripting tasks, and Python's more readable syntax, more os-independent os binding, and larger user community for support ensures that the final result will almost invariably be superior when written in Python than when written in scsh.)

Jeremy

Well, my current project is making a little script that runs regularly on my P133 Linux box in the basement, reads a file called .auto-backup that has filenames and config info in each /home/* directory that has one, and copying files over from the Windows machines that live in the house, while keeping older copies of the files, according to the config files.

It's a fairly easy task, except I've never really done useful file I/O before, and I'm trying to find an elegant way to parse the config file. Right now, my best solution uses global variables and loops that the control has to break out of, which juist feels icky.

When I get everything working, I'll nominate it as Arsware, but that's down the road.

I picked Python because I want to learn a well-known scripting langage, and Perl syntax scares me :). Python has all the handy os and file compare and run-in-shell libraries, so I'll probably stick with it, but the real programming (not just calling libraries) always seems kind of clumsy, compared to Scheme.

O'Caml sounds like fun, but I'll wait until I'm done with SICP, I think. I have enough to do as it is without picking up a third language; I have to get to know C++ for the AP exam come May.

Then again, I just got into college (w00t! :D ), so I can afford to do a little more of what I want.

The end of another long and rambling post.


(.mobius)
 
D

Deleted member 5324

Guest
Originally posted by .mobius:
It's a fairly easy task, except I've never really done useful file I/O before, and I'm trying to find an elegant way to parse the config file. Right now, my best solution uses global variables and loops that the control has to break out of, which juist feels icky.

Python has a standard ConfigParser module that might come in useful for that. I've never used it myself, but it might be useful.

O'Caml sounds like fun, but I'll wait until I'm done with SICP, I think. I have enough to do as it is without picking up a third language; I have to get to know C++ for the AP exam come May.

I thought they changed the AP exam to Java. I guess I was wrong :)

Then again, I just got into college (w00t! :D ), so I can afford to do a little more of what I want.

Congratulations! So where are you going?

Jeremy
 

.mobius

Ars Scholae Palatinae
1,139
Originally posted by jemfinch:
Python has a standard ConfigParser module that might come in useful for that. I've never used it myself, but it might be useful.

I'll look into that--it could certainly save a lot of work.

Quote: "
O'Caml sounds like fun, but I'll wait until I'm done with SICP, I think. I have enough to do as it is without picking up a third language; I have to get to know C++ for the AP exam come May.
"

I thought they changed the AP exam to Java. I guess I was wrong :)

They have, but it doesn't take effect until next school year. It's a good thing for me, because I already know the basics of C++.

Quote: "
Then again, I just got into college (w00t! :D ), so I can afford to do a little more of what I want.
"

Congratulations! So where are you going?

Jeremy

Well, either Caltech or University of Chicago. I can't make up my mind. It's tough, honest! :)

Hmm... I'm kind of hijacking the thread, so I'll try to keep it a little more on-topic from here on out, or start a new thread for the Python stuff. Thanks for the info, though, Jeremy.


(.mobius)
 

Afty

Ars Scholae Palatinae
1,434
Wow, the language for the AP is changing again? I took it in the next-to-last year of Pascal. What a useful language that was. Though I guess it did help me in my intro CS courses in college.

Man, I'm getting old. :(

Edit: Oops, I should make some attempt to keep this on topic. I used LISP a lot in my AI classes in college, and I don't think I ever got a good grasp of it. It's just a different thought process. I always ended up thinking in a procedural way and then translating it into a functional equivalent. And yes, I know that exactly what you're not supposed to do.

Functional programming does seem to be good for particular problems, though. I can see why everyone in AI research uses LISP.
 
Status
Not open for further replies.