Re[Haskell-cafe] [2]: Help with generalizing function

Jonathan Cast jonathanccast at fastmail.fm
Mon Jun 23 10:06:29 EDT 2008


On Mon, 2008-06-23 at 04:03 -0700, leledumbo wrote:
> >> Don't give up so fast !! (Note that you can't do what you asked for in
> >> Pascal either, in fact Pascal don't support n-uplet) 
> I'm not going to use n-uplet, dynamic array of array of Byte is enough.
> Though not very optimizing, I can use 2 step process:

> 1. Generate all lists (array of Byte) of length N which each element ranges
> from 0 to C.

Come now, you really think this is harder in Haskell than in Pascal?
Understanding Haskell takes a little work, granted, but then I always
thought the same was true of Pascal...

> 2. Filter which has sum=C.

Come now.  Try your browser's search engine on the standard prelude,
word by word.  You can't really expect this to be just given to you, but
you must realise that sometimes it is...

> 
> >> seems that leledumbo found a new way to force us give the answers to
> >> those homeworks LOL
> Don't worry, I'm not gonna use it because it has to be done in procedural
> way (I haven't taken Functional Programming class yet). The reason why I ask
> is I'm HOPING that if I can understand how it works, implementing the
> procedural form would be easy. I've read somewhere that functional language
> can be implemented in procedural one. In fact, GHC outputs C code.
> 
> Thanks for the answer. I'll use it to learn, it's quite difficult to switch
> from procedural to functional. I mean, in procedural it's easy to find the
> fastest solution using brute force.

Huh?  The brute force solution is usually the /slowest/ solution, even
in imperative languages.  Consider bubble sort (bogo sort if you want to
push it) vs. quick sort.

>  This can't be done in functional.

If you mean `find a brute-force solution quickly', getting to the brute
force solution is indeed usually /faster/ in a functional language (it's
usually shorter, too), because of the higher level of abstraction.  (The
same is true for other modern languages, such as Perl, Ruby, to a
certain extent Python, and soon enough (hopefully) PHP, but then it's
questionable in what way the languages (as opposed to their programming
cultures) fail to be functional in the first place).

jcc





More information about the Haskell-Cafe mailing list