6 ms·
I'm not scared. It just does not make sense to me. Just because I speak French and English fluently does not mean I can speak Japanese. My wife is Japanese, an
by chris_mahan 12y ago
I'm not scared. It just does not make sense to me.
Just because I speak French and English fluently does not mean I can speak Japanese. My wife is Japanese, and speaks it fluently. We've been together 19 years. I've tried to learn. it just does not make sense. It would be nice, because I have relatives there, and I could go live there, legally, but it just doesn't make sense in my brain.
What can I do?
Functional programming is the same. Every couple of years I take a look, and I recoil in confusion.
It also doesn't help that people make fun of me for seeming dumb.
- jeffasinger 12y agoWhat worked for me was taking functional programming concepts, and applying them in languages where I was already comfortable. That way, I wasn't learning a new syntax and a new programming paradigm at the same time.
- sanderjd 12y agoWhich concepts did you find the most applicable? For me it is applying functions to collections in different ways, eg. map, fold, etc.
- innguest 12y agoCan you say a few things about the parts that don't make sense to you? That would help me identify what beginners struggle with, as I'm developing tutorials for that reason.
- chris_mahan 12y agoIt's simple: the very concept of the program. I want to write a program that reads one text file, and outputs another. Let's say the first file contains a list of emails, one email per line. The second file should contain the list of email domains in the first file, one line per email domain. Procedurally, I would simply declare a list, read the file of emails, line by line, split each one on the @ symbol, and if the part on the right was not already on the list, I would add it to the list. At the end, I would write the list to a new file, one line per item in the list. How would one do that using functional programming?
- chris_mahan 12y agoBy the way, for those who think programs like that are too simplistic, and not a good showcase for all the FP goodness, maybe that's part of the problem. I work in the corporation, and I we do simple things.
- innguest 12y agoOk so here's one way to solve that problem in FP: We define what we have first (after all this is mathematics): EmailList = a list of Email Email = a string that goes "something" then "@" then "domain" Now what we want: DomainList, no duplicates. Ok so no we're set up. We want a function that takes us from an EmailList to DomainList, as defined above. There are no objects so we simply say the relationships between things. I will introduce the tools we need: map = takes a function and a list and applies that function to every element of the list, returning a new list. We use map over EmailList with the function extractDomain which we will write later (it's trivial to do without mutation so I won't show here). So now we have a list of domains, but there might be duplicates. So now I have DomainList(with duplicates) and I want DomainList(without duplicates), so I want a function from DomainList to DomainList. Well, what's the difference between what I have and what I want? What I have has too much, and I want less (ie. the list I have has too many domains (duplicates) and I want fewer (unique)). filter = takes a list and a function. That function it takes, that you will supply to filter, we'll call f. Function f takes one list element (so filter will call f for every element) and it must return a boolean to mean whether that element should go into the new list, or if it should not go into the list. So we could use filter to go from a DomainList with too much, to a DomainList with fewer things. However each time our function f is called, we don't know if this function has already been called before with that same domain; and because we don't have state to change a variable to keep a tally, it seems impossible. So let's approach the problem in a different way. One thing we could do is sort the list, then group adjacent similar items, then map over the list taking the first element of each item. But we won't do that. We need something more generic than filter. Let's look at fold. fold = takes a list, a function f, and a starting point z (that we call 'zero'). For each element of the list, it calls your supplied function f with the current element and the zero; but then, it would be useless because it would be the same zero all the time. So what fold does, to be useful, is that whatever the result of calling your function f with a list item and the zero is, THAT value (so the value the call to yourFunctionF(list_item, zero) returns) will be the next 'zero'. So now we have a little machine called fold that can thread something we have through its inner mechanism and play along with us. Using that we can say the zero is the list of elements we've seen so far. So now our DomainList -> DomainList function will be a fold. That fold will take a the DomainList we have, and a zero that will be the empty list (to start with!) and a function f. That function f will simply check if the item is inside zero. If it is, then simply return zero (because that will be the zero for the next call to f!); if it's not, then return a new zero with that item added to it. Now we've achieved the goal without any mutation (ie. without us talking/concerning ourselves with any mutation. of course there's lots of mutation going on in RAM but the point is not to eliminate that!) Does that help?
- agumonkey 12y agoFunctional programming is a hard compromise. Almost no mutation or side effects, every part of the system is encoded/encapsulated into a function entity. That's it. The fun part is that many languages end up aiming for it. Good multithreaded/reentrant code is almost functional. I just saw a ruby rant asking to avoid mutation as much as possible. Same goes for PHP PSR-x ... Functional programming suffers from acute mathematitis, the people behind it are fond of iterating too fast around their newfound concepts and it end up as cryptic as possible.
- blktiger 12y agoI have to say, it makes a big difference who you learn from (or perhaps how you learn). In college I had several courses where we programmed in Scheme and I hated every minute of them because I felt like Scheme made things harder and uglier with no tangible benefit. Then I had a class where we learned Haskell with one of the best professors in the department. He basically taught me all of the great things about functional programming and now I try to use concepts from that class in whatever language I'm programming in. Learning functional programming is paradigm-shifting and gives you a much broader set of tools to work with, but if it's not taught well it won't help you.