File path in Haskell

Find this useful? Support us: Star on GitHub 6
Category: File | Language: Haskell

In Haskell, you can get the file path using the __FILE__ and __LINE__ macros from the Debug.Trace module.

Here's an example:

import Debug.Trace

main :: IO ()
main = do
let filePath = trace (__FILE__ ++ " at line " ++ show __LINE__) "hello"
putStrLn filePath

When you run this program, it will output something like:

/path/to/file.hs at line 7
hello

This shows the file path and line number where the trace function was called. You can use this technique to add debugging information to your Haskell code.