Haskell – 142 – elementi base del linguaggio – 5

Continuo da qui, copio qui.

Files sorgenti
As programmers, we don’t want to simply evaluate small expressions like these — we want to sit down, write code in our editor of choice, save it and then use it.

We already saw […] how to write a Hello World program and how to compile it. OOPS, saltato, era solo introdittiva, meglio vederlo qui per bene. Here, we show how to use functions defined in a source-code file in the interactive environment. To do this, create a file called Test.hs and enter the following code:

Test.hs

module Test
    where

x = 5
y = (6, "Hello")
z = x * fst y

This is a very simple “program” written in Haskell. It defines a module called “Test” (in general module names should match file names; see the section on Modules for more on this [prossimamente]). In this module, there are three definitions: x, y and z. Once you’ve written and saved this file, in the directory in which you saved it, load this in your favorite interpreter [io GHC], by executing either of the following:

* hs-142 $ ghci Test.hs
GHCi, version 8.0.2: http://www.haskell.org/ghc/  :? for help
[1 of 1] Compiling Test             ( Test.hs, interpreted )
Ok, modules loaded: Test.
*Test>

This will start Hugs or GHCi, respectively, and load the file. Alternatively, if you already have one of them loaded, you can use the “:load” command (or just “:l“) to load a module as: :l Test.hs.

Between the first and last line, the interpreter will print various data to explain what it is doing. If any errors appear, you probably mistyped something in the file; double check and then try again.

You’ll notice that where it used to say “Prelude” it now says “Test“. That means that Test is the current module. The Prelude module (usually simply referred to as “the Prelude”) is always loaded and contains the standard definitions (for instance, the (:) operator for lists, or (+) or (*), fst, snd and so on).

Now that we’ve loaded Test, we can use things that were defined in it. For example:

*Test> x
5
*Test> y
(6,"Hello")
*Test> z
30

Perfect, just as we expected!

One final issue regarding how to compile programs to stand-alone executables remains. In order for a program to be an executable, it must have the module name “Main” and must contain a function called main. So, if you go in to Test.hs and rename it to “Main” (change the line that reads module Test to module Main), we simply need to add a main function.

Main.hs

module Main
    where

x = 5
y = (6, "Hello")
z = x * fst y

main = putStrLn "Hello World"

Now, save the file, and compile it […]. For example, in GHC, you would say:

* hs-142 $ ghc --make Main.hs -o main
[1 of 1] Compiling Main             ( Main.hs, Main.o )
Linking main ...

Il tutorial usa il nome Test ma credo sia in errore. Sarebbero da approfondire le opzioni; p.es. inquesto caso “--make” si può omettere.

This will create a file called “main” (or on Windows, “main.exe“) that you can then run.

* hs-142 $ ./main
Hello World

Pausa, prima di affrontare un argomento lungo.

đź‘˝

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: