Advice on Learning “How to Think Functional”?

后端 未结 5 2076
慢半拍i
慢半拍i 2021-01-31 11:56

As a newbie in functional languages (I started touching Erlang a couple of weeks ago -- the first functional language I could get my hands on).

I started to writing some

5条回答
  •  忘了有多久
    2021-01-31 13:00

    After a while, I found that functional programming […] encourages a "top down" design.

    I'm not sure this is an accurate statement. I've been recently trying to teach myself functional programming, and I've found that a sort "bottom-up" style of programming really helps me. To use your example of merge sort:

    • First look at the base case. How do you sort an array of 0/1 elements?
    • Next, look at the base + 1, base + 2, … cases. Eventually, you should see a pattern (splitting into subproblems, solving subproblems, combining subsolutions) that allows you to write a general recursive case than eventually reaches the base case.
    • Splitting into subproblems is easy, but combining the subsolutions is a bit harder. You need a way to merge two sorted arrays into one sorted array.
    • Now put everything together. Congratulations, you've just written merge sort. :)

    I could be misusing the term, but this feels like bottom-up design to me. Functional programming is different than object-oriented programming, but you shouldn't need to totally abandon existing design techniques when switched between the two.

提交回复
热议问题