关于学习的建议“如何认为功能”? [英] Advice on Learning "How to Think Functional"?

查看:100
本文介绍了关于学习的建议“如何认为功能”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为功能语言的新手(几个星期前我开始接触Erlang - 我可以得到我的第一个功能语言)。

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).

我开始编写一些小算法(例如 left_rotate_list bubble_sort, merge_sort 等)。我发现自己经常会迷失在诸如我应该使用帮助列表中间结果存储的决定?和我应该创建一个帮助函数来做到这一点吗?

I started to writing some small algorithms (such as left_rotate_list, bubble_sort, merge_sort etc.). I found myself often getting lost in decisions such as "should I use a helper List for intermediate result storage?" and "should I create a helper function to do this?"

过了一段时间,我发现功能编程(如果我在说话,跟我在一起没有意义总之)鼓励一个自上而下的设计:即当我做merge_sort时,你首先写下所有的合并排序步骤,并将它们命名为单独的帮助函数;然后你一个一个地实现这些帮助函数(如果你需要进一步划分这些帮助函数,那么用同样的方法)。

After a while, I found that functional programming (bear with me if what I am talking does not make sense at all) encourages a "top down" design: i.e., when I do merge_sort, you first write down all the merge sort steps, and name them as individual helper functions; and then you implement those helper functions one by one (and if you need to further dividing those helper functions, do it in the same approach).

这似乎与OO相矛盾设计一下,您可以从底部开始构建基本数据结构,然后将数据结构和算法组合到所需的位置。

This seems to contradict OO design a little, in which you can start from the bottom to build the basic data structure, and then assemble the data structure and algorithms into what you want.

感谢您的意见。是的,我想获得关于如何用功能语言思考的建议(就像Java中的思考,C ++中的思考)。

Thanks for comments. Yes, I want to get advice about how to "think in functional language" (just like "thinking in Java", "thinking in C++").

推荐答案


过了一段时间,我发现功能编程[...]鼓励自上而下的设计。

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:


  • 首先查看基本情况。你如何对0/1元素进行排序?

  • 接下来,看看基数+ 1,基数+ 2,...的情况。最后,您应该看到一个模式(分解为子问题,解决子问题,组合子解决方案),允许您编写一个通用的递归案例,而不是最终达到基本情况。

  • 分解到子问题很容易,但结合这些解决方案有点困难。您需要一种将两个排序的数组合并成一个排序的数组的方式。

  • 现在把所有内容都放在一起。恭喜,您刚刚编写合并排序。 :)

  • 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.

这篇关于关于学习的建议“如何认为功能”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆