Matthew Smillie
2/12/2006 6:02:00 AM
On Feb 12, 2006, at 5:23, Alex Combas wrote:
> For example:
> ~~~
> find_it
> Write a function find_it that takes an array of strings and a block.
> The block should take two parameters and return a boolean value.
> The function should allow to implement longest_string,
> shortest_string,
> and other functions by changing the block.
> ~~~
>
> Sorry, this just seems really muddled.
> Is find_it a class, method, or what?
> What is the point of the boolean that the block needs to return?
>
> I asume that find_it is a class, and longest_string and
> shortest_string
> are object methods, and the array and the block are passed when
> the class is initialized, but I feel like I'm missing something.
A function in this case would be a method. The two terms are often
used interchangeably, despite theoretical quibbles that they're not
quite the same thing; a lambda expression is also a function, for
instance.
The point of the block is that you're attempting to write a method
that can be used as a general implementation of several other
methods, depending on the contents of the block. The boolean return
value of the block is actually a very small hint, following from the
fact that finding the shortest/longest element of an array is very
closely related to sorting the array.
Anyway, here's the initial problem restated:
> Write a method find_it that takes as parameters:
> - an array of strings
> - a block
> The block should take two parameters and return a boolean value.
>
> You should be able to implement longest_string and shortest_string
> simply by changing the block provided to find_it. In other words,
> the implementation of longest_string and shortest_string will
> consist only of a call to find_it with a particular block:
>
> def longest_string (arr)
> find_it(arr) { ... }
> end
>
> def shortest_string (arr)
> find_it(arr) { ... }
> end
As for the document being muddled, I read a fair bit of it, and it
strikes me that the amount of apparent muddle would depend on your
previous exposure to programming in general.
It's a bit of a catch with teaching/learning programming; it uses
very jargony language without the same formalism you get in maths. A
method in one language is a function in another is a procedure in a
third and doesn't even exist as a concept in a fourth language. This
vocabulary issue really does put a hitch in learning programming,
since to know what a term really means, you need to know the theory
behind it, for which you need to know what the terms mean.... and I
think you see the problem. The best way to deal with this obstacle
it is to jump right in, and ask questions when you're stumped. Which
you seem to be doing, so good on you.
Hope that helps.
matthew smillie.
----
Matthew Smillie <M.B.Smillie@sms.ed.ac.uk>
Institute for Communicating and Collaborative Systems
University of Edinburgh