|
|
scott wrote:
> So actually, you had the same thought to start with (I want to sum all
> the elements) and the end result is the same assembly code. So the only
> real point is if you find writing out and thinking about the first
> solution easier than the second or not. In your own words:
>
> "3. The second version looks like some kind of a riddle. Not a hard
> riddle, but a somewhat baffling definition all the same."
>
> So why on Earth bother with the riddle in the first place?
If all you're trying to do is sum all the elements in a list, neither
approach has a really compelling advantage or disadvantage. I used it as
an example of a simple task that anybody can easily understand, and used
it to illustrate the difference in mentallity between the two approaches.
> Here's a couple of changes you can try to make to the Haskell version:
>
> 1) Modify the code so that it stores the sum up to each element in that
> element.
Uh, OK.
sums xs = sums' 0 xs
where
sums' t [] = [t]
sums' t (x:xs) = t : sums' (t+x) xs
Takes a list of numbers, returns a list of intermediate sums. The first
element is always 0, the last element is always the total sum of the
entire list.
> 2) Modify the code so that it sums up nodes in a tree structure,
> starting from any child.
Erm... I don't really understand what you mean.
> All very intuitive, requiring little modification to the sum function.
Similarly, in Haskell I can define a single function that iterates over
a list, and then write 1-linears that will find the sum / product /
minimum / maximum of the list, or the logical AND / OR of a list of
booleans, or concatenate a list of lists, or... All very intuitive and
easy to write once you have the iteration function.
--
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*
Post a reply to this message
|
|