[Haskell-beginners] Noobie attempt to process log output into dependency graph

Magnus Therning magnus at therning.org
Sat Dec 17 01:03:21 UTC 2016


John Lusk <john-haskell at how-hard-can-it-be.com> writes:

> Thanks, all, that gives me something to chew on.
>
> It occurred to me (during my 45-minute commute to work) that all
> Haskell programs (listen to the noob <eyeroll/>) have the following
> structure (modulo my fractured syntax):
>
> main :: IO()
> main = do
>   inputs <- getInputs
>   doOutput $ f inputs initialState
>
> f :: [input] -> state -> outputs
>
> f [] state =
>   transformToOutputs state
>
> f (input:inputs) state =
>   f inputs (newState state input)
>
> doOutput :: [output] -> IO()
>
> doOutput outputs = do
>   putStr $ unlines outputs
>
> So all I have to do is write newState and I'm good! ^_^
>
> (transformToOutputs will, of course, be a snap.)
>
> Right?

Very many do, yes. One thing though, it is worth thinking about the
order of arguments. I often order it

     f state [] = ...
     f state (x:xs) = ...

because that fits better with `foldl` and `map` :)

/M

--
Magnus Therning              OpenPGP: 0x927912051716CE39
email: magnus at therning.org   jabber: magnus at therning.org
twitter: magthe               http://therning.org/magnus

The early bird may get the worm, but the second mouse gets the cheese.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 832 bytes
Desc: not available
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20161217/b59d7c46/attachment.sig>


More information about the Beginners mailing list