Haskell – 163 – Moduli – 3

Continuo da qui, copio qui.

Letterale e non letterale
The idea of literate programming is a relatively simple one, but took quite a while to become popularized. When we think about programming, we think about the code being the default mode of entry and comments being secondary. That is, we write code without any special annotation, but comments are annotated with either -- or {- ... -}. Literate programming swaps these preconceptions.

There are two types of literate programs in Haskell; the first uses so-called Bird-scripts and the second uses LaTeX-style markup. Each will be discussed individually. No matter which you use, literate scripts must have the extension lhs instead of hs to tell the compiler that the program is written in a literate style.

Bird-scripts
In a Bird-style literate program, comments are default and code is introduced with a leading greater-than sign (“>“). Everything else remains the same. For example, our Hello World program would be written in Bird-style as:

bs-hw.lhs

This is a simple (literate!) Hello World program.

> module Main
>     where

All our main function does is print a string:

> main = putStrLn "Hello World"
$ runghc bs-hw.lhs
Hello World

e nella REPL

Prelude> :l bs-hw.lhs
[1 of 1] Compiling Main             ( bs-hw.lhs, interpreted )
Ok, modules loaded: Main.
*Main> main
Hello World

Note that the spaces between the lines of code and the “comments” are necessary (your compiler will probably complain if you are missing them). When compiled or loaded in an interpreter, this program will have exactly the same properties as the non-literate version from the section on Files [qui e posts successivi].

LaTeX-scripts
LaTeX is a text-markup language very popular in the academic community for publishing. If you are unfamiliar with LaTeX, you may not find this section terribly useful.

Again, a literate Hello World program written in LaTeX-style would look like:

lt-hw.lhs

This is another simple (literate!) Hello World program.

\begin{code}
module Main
    where
\end{code}

All our main function does is print a string:

\begin{code}
main = putStrLn "Hello World"
\end{code}
$ runghc lt-hw.lhs
Hello World

e

Prelude> :l lt-hw.lhs
[1 of 1] Compiling Main             ( lt-hw.lhs, interpreted )
Ok, modules loaded: Main.
*Main> main
Hello World

In certe occasioni particolari potrebbe essere utile, specie la versione LaTeX. Inoltre nel :load non è necessario specificare l’estensione:

Prelude> :l lt-hw
[1 of 1] Compiling Main             ( lt-hw.lhs, interpreted )
Ok, modules loaded: Main.

👽

Posta un commento o usa questo indirizzo per il trackback.

Trackback

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo di WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione /  Modifica )

Google photo

Stai commentando usando il tuo account Google. Chiudi sessione /  Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione /  Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione /  Modifica )

Connessione a %s...

Questo sito utilizza Akismet per ridurre lo spam. Scopri come vengono elaborati i dati derivati dai commenti.

%d blogger hanno fatto clic su Mi Piace per questo: