Saturday, 15 March 2014

Diagnostic output in Haskell -



Diagnostic output in Haskell -

i'm new haskell, far, lot. learned programming in imperative languages, c++ , c, functional approach new me.

when writing functions/methods before, took 'incremental' approach (as people do): writing little part of code, checking if results far expected (usually printing them stdout printf or std::cout), refining algorithm, enhancing algorithm, checking if results far expected (usually printing them stdout printf or std::cout), refining the… seldom wrote whole methods in 1 piece.

essential 'incremental' approach ability have diagnostic output (printf or std::cout in illustration above). in haskell (as far understand of now), i'd have alter signature of function if want - - utilize 'putstrln' write stdout, because 'putstrln' returns io monad contains info want printed, not print in moment of invoking 'putstrln', right? every time want utilize 'putstrln' diagnostic output, i'd have alter signature of current function , way other functions phone call etc…

so there inexpensive , easy way print value of 'local variable' of function standard output?

or mere fact i'm asking sign i'm not understanding fundamental part of programming in haskell? think have grasp on concepts of imperative programming (at to the lowest degree in c/c++ , java etc…); think 'know how program', haskell quite hard me right now...

thank much :)

this weird, because find without read eval print loop (repl) in languages you're used to, i'm forever frustrated how much work test code go along. repl fundamental incremental code development; can utilize test code without having add together bunch of print statements.

have ghci open @ same time editor. write smaller, single-purpose functions. seems bizzare @ first, function application basic unit of work in haskell , doesn't have sort of overheads in imperative languages. each time write function, :r in ghci , test variety of input. haskell dense, counts worth making separate function much shorter on screen you're used to.

occasionally end stuck in lengthy monadic computation or something. ghci lets set breakpoints - utilize these in preference adding print statements code because can mess , investigate bit more without editing code, , importantly, don't need add together show constraints on type signatures.

when you're done, can manually inline gratuitously short helper functions , compile ghc -o2.

(using manually added print statements, or debug.trace module finish pain compared in experience.)

summary: whenever possible, avoid editing code while testing it. utilize ghci lot.

haskell

No comments:

Post a Comment