
Is there a generally accepted "better" way of dealing with the following: say I have three functions that all rely on output from each other. Ie, we get raw input, which one function processes. another function then requires the output from this function and produces some other output, which a third function then requires. would it be better to implement the functions like this: f1 :: a -> b f2 :: b -> c f3 :: c -> d final :: a -> d or like this: f1 :: a -> b f2 :: a -> c f3 :: a -> d Ie, in the latter set, f2 calls f1 on its input, and then proceeds to process it. f3 calls f2 on its input, etc. In the former case, final would just chain all three functions together itself (or I might just do it in main or somewhere else that's appropriate) Are one of these methods generally considered a better practice than the other, specifically in a functional context? My gut tells me that the former method looks more appropriate, but I'm not sure if it'd really be the accepted practice regardless of context, or if one might use either of these methods depending on the specifics of what they're doing. Or if it's just a matter of taste/opinion?

Hello Alec The first version composes whereas the second one doesn't. With the first version you should be able to write final like this: final :: a -> d final x = f3 $ f2 $ f1 x or pointfree final :: a -> d final = f3 . f2 . f1 With composable functions you may find the intermediate steps f1, f2 & f3 are useful in other contexts. Best wishes Stephen

No, ya, I get that differences between the two methods, I was asking if one of them was generally considered better practice than the other.
With composable functions you may find the intermediate steps f1, f2 & f3 are useful in other contexts.
Ya, that was what prompted me to think about this in the first place
(needing to use an intermediate value in another context when I had
set up the functions in the latter fashion). Does this fact make
writing the functions like that better practice though, or is it
pretty much left up to context as to which of the two methods to use?
On Sat, Sep 4, 2010 at 3:55 AM, Stephen Tetley
Hello Alec
The first version composes whereas the second one doesn't. With the first version you should be able to write final like this:
final :: a -> d final x = f3 $ f2 $ f1 x
or pointfree
final :: a -> d final = f3 . f2 . f1
With composable functions you may find the intermediate steps f1, f2 & f3 are useful in other contexts.
Best wishes
Stephen _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Hi Alec I think with experience its second nature for people to do the former - I think by that you could call it best practice. With the second version you might also find it hard naming the intermediate steps for real code that does things, this should be an indication that the first approach is better...

On Sat, Sep 4, 2010 at 10:06 AM, Alec Benzer
No, ya, I get that differences between the two methods, I was asking if one of them was generally considered better practice than the other.
I think your question is mixing two different concern : A) how you should write your Haskell functions so that you reap most benefit from its functional nature, and B) what functions you should expose in the interface of your libraries. The answer to A) is that you should use the solution that is more composable (the former), the answer to B) really depends on what your library does and the philosophy of your interface... In other words, it may be better in your specific case to expose the latter functions (probably not often but it may happen) but the internals of your library should most likely be written in the former style (it is easy to form the latter functions from the former, while the opposite is quite impossible). -- Jedaï

A) is all I was really asking. I'm not writing a library or exposing
any functions. I was concerned with how I should write the internals,
although I see how in certain contexts the latter form may be
preferable if I was writing a library.
On Sun, Sep 5, 2010 at 3:41 AM, Chaddaï Fouché
On Sat, Sep 4, 2010 at 10:06 AM, Alec Benzer
wrote: No, ya, I get that differences between the two methods, I was asking if one of them was generally considered better practice than the other.
I think your question is mixing two different concern : A) how you should write your Haskell functions so that you reap most benefit from its functional nature, and B) what functions you should expose in the interface of your libraries.
The answer to A) is that you should use the solution that is more composable (the former), the answer to B) really depends on what your library does and the philosophy of your interface... In other words, it may be better in your specific case to expose the latter functions (probably not often but it may happen) but the internals of your library should most likely be written in the former style (it is easy to form the latter functions from the former, while the opposite is quite impossible).
-- Jedaï

The first version. It is generally considered a good practice to Increase
Cohesion and Decrease Coupling. Its a very common approach, google it.
On Sun, Sep 5, 2010 at 4:50 AM, Alec Benzer
A) is all I was really asking. I'm not writing a library or exposing any functions. I was concerned with how I should write the internals, although I see how in certain contexts the latter form may be preferable if I was writing a library.
On Sun, Sep 5, 2010 at 3:41 AM, Chaddaï Fouché
wrote: On Sat, Sep 4, 2010 at 10:06 AM, Alec Benzer
wrote: No, ya, I get that differences between the two methods, I was asking if one of them was generally considered better practice than the other.
I think your question is mixing two different concern : A) how you should write your Haskell functions so that you reap most benefit from its functional nature, and B) what functions you should expose in the interface of your libraries.
The answer to A) is that you should use the solution that is more composable (the former), the answer to B) really depends on what your library does and the philosophy of your interface... In other words, it may be better in your specific case to expose the latter functions (probably not often but it may happen) but the internals of your library should most likely be written in the former style (it is easy to form the latter functions from the former, while the opposite is quite impossible).
-- Jedaï
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- Federico Mastellone Ingeniero Informática - ITBA ".. there are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult." Tony Hoare, 1980 ACM Turing Award Lecture.
participants (4)
-
Alec Benzer
-
Chaddaï Fouché
-
Federico Mastellone
-
Stephen Tetley