Hello people. I'm looking for FP examples that are very simple (very few lines, one-liners are even better) and that totally look like sorcery to a traditional imperative programmer. Can you help me with that? I want to show some old school programmers who are friends of mine but keep saying that all programming is exactly the same and that every single language in existence is a botchered, downgraded version of C. They also say that once you learn how to think the C way you'll be able to program in every other single language because they are mostly the same.

I am just starting out in FP, so I could only come up with a tail-recursive factorial and it's got them near brain-explosion mode because they think there is only one possible type of recursion.

Any other cool weid-looking examples?

I have to agree that the basics do help when you learn a new language. My first was Fortran which at the time there was no C language but there was Fortran, PL/1, Cobol and such.

What about testing for equality? You would think that's a basic thing you learn in C then get blown out of the water in JavaScript (JS.)
Watch https://www.youtube.com/watch?v=hQVTIJBZook and move to the 15 minute mark to see where a C programmer may get burned.

commented: [] + [] == "" yep, sounds about right +7

Not a functional language, but Prolog is pretty cool.

hanoi(1,FROM,TO,_) :- write('disk from '),
                      write(FROM),
                      write(' to '),
                      write(TO),
                      nl.
hanoi(N,FROM,TO,X) :- N > 1,
                      NMIN1 is N - 1,
                      hanoi(NMIN1,FROM,X,TO),
                      hanoi(1,FROM,TO,_),
                      hanoi(NMIN1,X,TO,FROM).
hanoi(N) :- hanoi(N,a,c,b).

Would solve the tower of hanoi problem for any number of disks. You would run it with a command like:

hanoi(3,left,right,center). 

And it would print:

disk from left to right
disk from left to center
disk from right to center
disk from left to right
disk from center to left
disk from center to right
disk from left to right

true

Its true power however lies in generation. Like generating a sudoku (live example) in what's technically 3 lines.

commented: Remember Borland and all the compilers they had? Prolog was one of them. +12

They also say that once you learn how to think the C way you'll be able to program in every other single language because they are mostly the same.

That statement is complete nonsense.

Learning your first programming language can be hard (and that's just learning, not mastering). Learning a second language (just like human languages) can be difficult because you will be thinking in "language A" idioms. After that it gets easier (your brain has been stretched).

And that's not even considering the other stuff you might need to know to be useful like SQL (for database applications) or the IDE (if there is one specific for the language).

However, just because you can "think" in C doesn't in any way prepare you for Erlang, Forth, etc. Consider that in APL, summing all the elements of a vector, a looks like

+/a

and if you are familiar with Conway's Game of Life, here it is in one butt-ugly line of APL

APLLife.gif

There's absolutely no way that C can prepare you for that.

commented: My code is ugly, but that's life. (me!) +0
commented: Let alone the ones made to be different, like COW and brainf**k +7

Oh my god! Are you sure you're not actually summoning Cthulu with that line of code, Jim? That's a really good example. And yes I agree that statement is nonsense. They are basically saying they know everything there is to come.

I took APL in my second year of Comp Sci and loved it. I used it on almost all my numerical analysis courses after that (I still play with it from time to time). I actually went through the Life code operator by operator to see what was happening at each step.

But I only gave that example to show how "unlike" c a language can be. If anyone is interested, there is an excellent free version (the one I use) available at nars2000.

commented: So few weekends, so much to check out. Faved for later! +0

Have a look at this:(Traevel pointed this one out)

++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.

Thought this was banned from the net, but you can find some explanation here.

Does not make me think of C, but it's hello world!

A typical functional programming language is F#. Get an overview of the syntax in a couple of minutes and read this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.