metaprogramming - How can one print the name of an identifier or binding in Haskell? -
supose want print name , value of "variable" in haskell. name known @ compile time! there better way in following example?
module v (e, c, ev, h, hbar, nm, k, viewallconstants) import text.printf c = 2.99792458e8::double e = exp(1) ev = 1.602176565e-19 h = 6.62606957e-34 hbar = h/(2*pi) nm = 1e-9 k = 1.3806488e-23 viewallconstants = putstr ((\a b -> (foldl (++) "" ( zipwith (++) (map (printf " = %.2e\n") b)))) ["c", "e", "ev", "h", "hbar", "nm", "k"] [c, e, ev, h, hbar, nm, k] )
please post working code example (runhaskell) in answer!
you can it! can't you're thinking. language implementations use dictionaries represent environments in expressions evaluated. of more foolish ones expose mechanism programmers. foolish because prevents implementors switching faster techniques in future. haskell not that, except maybe in type checker. doesn't stop building , using such dictionaries yourself!
if you're dealing bunch of things of same type, you're in luck—you can use map
or trie connect names values. if need things of various types, life gets more complicated—see hlist records example.
all said, think should think how want this. it's kind of unusual.
Comments
Post a Comment