Process to pass from problem to code. How did you learn?

后端 未结 13 1752
离开以前
离开以前 2020-11-30 09:40

I\'m teaching/helping a student to program.

I remember the following process always helped me when I started; It looks pretty intuitive and I wonder if someone else

相关标签:
13条回答
  • 2020-11-30 10:13

    I start at the top and work my way down. Basically, I'll start by writing a high level procedure, sketch out the details inside of it, and then start filling in the details.

    Say I had this problem (yoinked from project euler)

    The sum of the squares of the first ten natural numbers is, 1^2 + 2^2 + ... + 10^2 = 385

    The square of the sum of the first ten natural numbers is, (1 + 2 + ... + 10)^2 = 55^2 = 3025

    Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 385 = 2640.

    Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

    So I start like this:

    (display (- (sum-of-squares (list-to 10))
                (square-of-sums (list-to 10))))
    

    Now, in Scheme, there is no sum-of-squares, square-of-sums or list-to functions. So the next step would be to build each of those. In building each of those functions, I may find I need to abstract out more. I try to keep things simple so that each function only really does one thing. When I build some piece of functionality that is testable, I write a unit test for it. When I start noticing a logical grouping for some data, and the functions that act on them, I may push it into an object.

    0 讨论(0)
提交回复
热议问题