Hello!

On Wed, Jun 02, 1999 at 08:12:04AM +0200, Friedrich Dominicus wrote:
> [...]

> this seems to to the thing I would like it to do. I now have to check if
> the given fn is valid and raise an error if not so I do think I'll make
> it;-)

No you don't have to check fn. readFile checks and throws an exception
if the file doesn't exist or is not readable. As you don't have an
exception handler in your definition, the error is propagated outwards
to the calling code, which is probably a good thing to do.

> [...]

> So please allow me another question. Is that a way such functions are
> build? How it that reused? Does it make sense to write some extra
> functions for intermediate steps or should I try to learn that kind of
> programming.


> I have some problems with it because  a lot of work is just done in one
> Method. My knowledge comes from OO-programming and there it wouldn't be
> good style to do so much in one function.

Then split it up like you'd do in an OO language. I think, FP also
is good for writing small functions that do one thing well, and then
composing them in various ways (as you see, composing functions (and
perhaps also values) in Haskell is possible in very many various ways :-) ).

In the running example, I could imagine those auxiliary functions:

splitFilterMap unSplitFn afterMap filterPredicate beforeMap splitFn =
  unSplitFn . map afterMap . filter filterPredicate . map beforeMap . splitFn
-- perhaps make that more generic by using fmap and mfilter
-- with this definition (that seems to be something generic enough for
-- the Monad library...):
-- mfilter :: MonadPlus m => (a -> Bool) -> m a -> m a
-- mfilter fn m = m >>= (\a -> if fn a then mzero else return a)

splitFilter unSplitFn filterPred splitFn =
  splitFilterMap unSplitFn id filterPred id splitFn

lenGt limit list = length list > limit

processFile limit = splitFilter unlines (lenGt limit) lines
-- for the unnumbered version
  OR

numberElems = zip [1..]

number2str (nr,l) = show nr ++ '\t' : l

processFile limit = splitFilterMap unlines number2str (lenGt limit . snd)
  id (numberElems . lines)

> Regards
> Friedrich

Regards, Hannah.


Reply via email to