Functional Programming

Programming, for all ages and all languages.
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Functional Programming

Post by Alboin »

I've been exploring different methods of programming, as I have become somewhat 'bored' with C and it's {} type languages. Being such, I have found functional languages like Haskell, lisp, etc. interesting. Anyone here have any experience with these? What do you think of them?
C8H10N4O2 | #446691 | Trust the nodes.
User avatar
Colonel Kernel
Member
Member
Posts: 1437
Joined: Tue Oct 17, 2006 6:06 pm
Location: Vancouver, BC, Canada
Contact:

Post by Colonel Kernel »

I used Lisp in an AI course. It was refreshingly minimalist in its syntax, but it was difficult to understand complex expressions. I was (supposedly) taught Haskell in a comparative programming languages course, but I had a lot of trouble absorbing it. I found the syntax very confusing -- especially the lack of parentheses around certain (but not all) function calls. (I might be remembering this wrong, but I do know I was very confused at the time.) The functional language I was most comfortable with back in school was probably ML.

A really interesting language to keep an eye on is Scala. It is an OO-functional hybrid with a syntax that mixes elements of Java, Haskell, and Ruby. There are two Scala compilers that can target either the JVM or CLR, so you can use Java or .NET libraries easily.

The functional features are interesting, although they don't go as deep as in other functional languages (e.g. -- no native support for continuations, no restrictions on the use of mutable state and side effects, etc.) The OO features are really interesting as they favour mixin-based composition over traditional inheritence of classes and interfaces.

Although the Scala people never draw this comparison, Scala reminds me a lot of what C++ is trying to be. They both support operator overloading, mixins (templates in C++ are great for mixins), functional features (at least pseudo-functional in the case of STL/Boost libraries for C++), etc. The difference being that Scala doesn't need a hacky compile-time template system, and it compiles to verifiably type-safe code.
Top three reasons why my OS project died:
  1. Too much overtime at work
  2. Got married
  3. My brain got stuck in an infinite loop while trying to design the memory manager
Don't let this happen to you!
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

I've learned Haskell as an integral part of my BSc (which i try to complete by next winter).

Haskell (and for that matter, functional languages) have a different fundament of programming. The main problem i have seen with that is that some people have trouble switching from a command-oriented language like C, Pascal, Java.

Once you got past that, you'll see that functional languages provide a different way of solving problems, and when you get more experienced, you can use the paradigm to your advantage.
There are times i'd like to insert a few lines of Haskell into my c code, because it can do some things very clean while the C solution would be rather messy.

On the other hand, Haskell can be rather confusing with I/O. And in the end, its just a matter of purpose wether to choose haskell and co or C and co. Since I do a lot in the interactive programs, my choice is usually on the latter.
Still, i've poked around the ghc (haskell compiler) site more than once and have recently found that it can compile to C code... i.e. mixing code suddenly sounds like an option :)

IMHO, people should pick a pure functional language and be enlightened by a different look on the world (wether they dispose of it afterwards, I dont care)
just my $.02
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
spiner
Posts: 3
Joined: Mon Oct 02, 2006 4:32 am
Contact:

Post by spiner »

I used Lisp for a long time many years ago and I'm still in love with it.
Sometimes difficult to learn, but it's well worth!!

Go for it!
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re: Functional Programming

Post by Candy »

Alboin wrote:I've been exploring different methods of programming, as I have become somewhat 'bored' with C and it's {} type languages. Being such, I have found functional languages like Haskell, lisp, etc. interesting. Anyone here have any experience with these? What do you think of them?
I only did a basic thing in haskell, but you might be interested to know that C++ also embeds a functional language - templates. I would use a variadic template patch for most similarity, but it's pretty similar already.

Code: Select all

template <int T>
class fib {
   enum { value = T * fib<T-1>::value; }
};

template <>
class fib<0> {
   enum { value = 1; }
};

int main() {
  printf("%d\n", fib<8>::value);
}
Compile this to assembly code and behold - no computation, but the actual value already computed by your compiler.


For certain types of generic solving mechanisms I like to think functionally and then implement in any language I see fit. I haven't messed with haskell enough to like it enough to implement something useful in.
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Re: Functional Programming

Post by Alboin »

Candy wrote:I only did a basic thing in haskell, but you might be interested to know that C++ also embeds a functional language - templates. I would use a variadic template patch for most similarity, but it's pretty similar already.
Yeah, I know about templates, but I am trying to pick up a functional language for the supposed "profound enlightenment experience you will have when you finally get it;". I just find imperative languages many times repetitive and so close to the hardware. It's like digging a crater with a spoon. Moreover, I can't find C++ any better. I've use it; Worked with it. Yet, it's essentially saying, "Look! I can have the machine make me organize my code by using these things called classes."

I think Greenspun's Tenth Rule of Programming fits what I'm saying: "Any sufficiently complicated C or Fortran program contains an ad hoc informally-specified bug-ridden slow implementation of half of Common Lisp."

PostScript: Thanks for the suggestions, I think I'm going to attempt Haskell, due to Lisp's somewhat decentralization. (Haskell really doesn't have this either, but I feel that there is more of it than there is for Lisp.)
C8H10N4O2 | #446691 | Trust the nodes.
User avatar
Colonel Kernel
Member
Member
Posts: 1437
Joined: Tue Oct 17, 2006 6:06 pm
Location: Vancouver, BC, Canada
Contact:

Re: Functional Programming

Post by Colonel Kernel »

Candy wrote:Compile this to assembly code and behold - no computation, but the actual value already computed by your compiler.
Unless the recursion is too deep and the compiler crashes. Yes, this has happened to me with production code. :twisted:
Top three reasons why my OS project died:
  1. Too much overtime at work
  2. Got married
  3. My brain got stuck in an infinite loop while trying to design the memory manager
Don't let this happen to you!
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Functional Programming

Post by Combuster »

Alboin wrote:PostScript: Thanks for the suggestions, I think I'm going to attempt Haskell, due to Lisp's somewhat decentralization. (Haskell really doesn't have this either, but I feel that there is more of it than there is for Lisp.)
you may want to have a look at the following sites:
http://www.haskell.org/ghc - the most common haskell compiler/interpreter
http://www.cs.uu.nl/helium/ - ghc without the "advanced" stuff (that includes the cryptic error messages :wink: )
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Crazed123
Member
Member
Posts: 248
Joined: Thu Oct 21, 2004 11:00 pm

Post by Crazed123 »

I've learned some Common Lisp. I never really groked continuations, so I couldn't figure out most Scheme.

Still, learn a Lisp. They're fun and, in most cases, impure enough that you don't have to learn The Ever-Dubious Beauty of Monads just to perform I/O.

Just remember: everything is an expression. A function is just another object. A function can carry its lexical environment around with it - functions that do so are called closures. A macro is simply a function that runs at compile-time, takes Lisp forms as its arguments and returns a linguistically valid Lisp form that the compiler embeds into its copy of your code where the macro call was.

If you like experimenting, however, you'll quickly run into Lisp's limits. Fortunately, they're miles above C's limits and really only make sense in terms of functional programming. They don't show up too much when solving actual problems.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re: Functional Programming

Post by Candy »

Colonel Kernel wrote:
Candy wrote:Compile this to assembly code and behold - no computation, but the actual value already computed by your compiler.
Unless the recursion is too deep and the compiler crashes. Yes, this has happened to me with production code. :twisted:

Code: Select all

testt.cc:4: error: template instantiation depth exceeds maximum of 500 (use -ftemplate-depth-NN to increase the maximum) instantiating `struct x<9999499>'


candy@blackbox:~$ time g++ -ftemplate-depth-1000 -o testt testt.cc
testt.cc:4: error: expected `}' before ';' token
testt.cc:4: error: expected unqualified-id before '}' token
testt.cc:7: error: expected `}' before ';' token
testt.cc:7: error: expected unqualified-id before '}' token

real    0m0.443s
user    0m0.376s
sys     0m0.019s
candy@blackbox:~$ jove testt.cc
candy@blackbox:~$ time g++ -ftemplate-depth-3000 -o testt testt.cc
testt.cc:4: error: expected `}' before ';' token
testt.cc:4: error: expected unqualified-id before '}' token
testt.cc:7: error: expected `}' before ';' token
testt.cc:7: error: expected unqualified-id before '}' token

real    0m6.032s
user    0m5.506s
sys     0m0.047s
candy@blackbox:~$ jove testt.cc
candy@blackbox:~$ time g++ -ftemplate-depth-10000 -o testt testt.cc
testt.cc:4: error: expected `}' before ';' token
testt.cc:4: error: expected unqualified-id before '}' token
testt.cc:7: error: expected `}' before ';' token
testt.cc:7: error: expected unqualified-id before '}' token
g++: Internal error: Segmentation fault (program cc1plus)
Please submit a full bug report.
See <URL:http://gcc.gnu.org/bugs.html> for instructions.

real    0m27.484s
user    0m25.964s
sys     0m0.115s
candy@blackbox:~$
How?
User avatar
Colonel Kernel
Member
Member
Posts: 1437
Joined: Tue Oct 17, 2006 6:06 pm
Location: Vancouver, BC, Canada
Contact:

Post by Colonel Kernel »

I was using VC++ 2003. It kinda sucks when it comes to template scalability.
Top three reasons why my OS project died:
  1. Too much overtime at work
  2. Got married
  3. My brain got stuck in an infinite loop while trying to design the memory manager
Don't let this happen to you!
ehird
Member
Member
Posts: 214
Joined: Thu Mar 15, 2007 8:48 am

Post by ehird »

Why Haskell Rocks:

Code: Select all

inf = 1:inf
main = putStrLn inf
[1, 1, 1, 1, 1, 1, 1, 1, 1...
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Post by Candy »

ehird wrote:Why Haskell Rocks:

Code: Select all

inf = 1:inf
main = putStrLn inf
[1, 1, 1, 1, 1, 1, 1, 1, 1...
IIRC, you could do the same by saying
[1, 1 ..] or some such. Why you would want an infinite row of ones and whether you couldn't do the same in C++, I doubt.
ehird
Member
Member
Posts: 214
Joined: Thu Mar 15, 2007 8:48 am

Post by ehird »

You absolutely cannot achieve that in C(++). Barely any languages are lazy-evaluated.
User avatar
~
Member
Member
Posts: 1228
Joined: Tue Mar 06, 2007 11:17 am
Libera.chat IRC: ArcheFire

Post by ~ »

If the interpreter was programmed in other language, then it can be done in any other, it's just that it must be programmed manually, but it can be done.

And, is that a way of telling to add the variable to itself recursive and infinitely and then printing that? Wouldn't be the same to program a loop in any other language to produce the very same result regardless the syntax used?
Post Reply