Functional Programming
Functional Programming
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.
- Colonel Kernel
- Member
- Posts: 1437
- Joined: Tue Oct 17, 2006 6:06 pm
- Location: Vancouver, BC, Canada
- Contact:
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.
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:
- Too much overtime at work
- Got married
- My brain got stuck in an infinite loop while trying to design the memory manager
- Combuster
- 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:
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
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
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!
Sometimes difficult to learn, but it's well worth!!
Go for it!
/Spiner
http://www.spiner.it
http://www.spiner.it
Re: Functional Programming
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.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?
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);
}
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.
Re: Functional Programming
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."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.
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.
- Colonel Kernel
- Member
- Posts: 1437
- Joined: Tue Oct 17, 2006 6:06 pm
- Location: Vancouver, BC, Canada
- Contact:
Re: Functional Programming
Unless the recursion is too deep and the compiler crashes. Yes, this has happened to me with production code.Candy wrote:Compile this to assembly code and behold - no computation, but the actual value already computed by your compiler.
Top three reasons why my OS project died:
- Too much overtime at work
- Got married
- My brain got stuck in an infinite loop while trying to design the memory manager
- Combuster
- 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
you may want to have a look at the following sites: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.)
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 )
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.
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.
Re: Functional Programming
Colonel Kernel wrote:Unless the recursion is too deep and the compiler crashes. Yes, this has happened to me with production code.Candy wrote:Compile this to assembly code and behold - no computation, but the actual value already computed by your compiler.
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:~$
- Colonel Kernel
- Member
- Posts: 1437
- Joined: Tue Oct 17, 2006 6:06 pm
- Location: Vancouver, BC, Canada
- Contact:
Why Haskell Rocks:
[1, 1, 1, 1, 1, 1, 1, 1, 1...
Code: Select all
inf = 1:inf
main = putStrLn inf
IIRC, you could do the same by sayingehird wrote:Why Haskell Rocks:
[1, 1, 1, 1, 1, 1, 1, 1, 1...Code: Select all
inf = 1:inf main = putStrLn inf
[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.
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?
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?