What extension should I use for a Haskell file?
Two common extensions are .hs
and .lhs
. The difference is in how the compiler handles comments. In a .hs
file, comments begin with --
or are enclosed in {-
/-}
pairs.
{- This is a multiline comment
about my factorial function
-}
-- It's simple using builtins
factorial n = product [1..n]
In a .lhs
file, every line is considered a comment, unless it is explicitly marked as code. There are two different styles you can use,
although you must use only one within a single file. First, you can mark
lines of code by prefix them with >
:
In this file, we will implement factorial.
> factorial :: (Enum a, Num a) => a -> a
> factorial n = product [1..n]
To embed code in a file that can be processed by LaTeX to produce
nice looking documentation, code can instead appear in a code
block:
In this file, we will implement factorial.
\begin{code}
factorial :: (Enum a, Num a) => a -> a
factorial n = product [1..n]
\end{code}
Both are equivalent to the following .hs
file:
-- In this file, we will implement factorial
factorial :: (Enum a, Num a) => a -> a
factorial n = product [1..n]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With