sto attraversando i problemi del libro O'Reilly Haskell. Il problema su cui sto lavorando èCominciando Haskell - ottenendo "non nel campo di applicazione: i dati del costruttore" Errore
Using the binary tree type that we defined earlier in this chapter,
write a function that will determine the height of the tree. The height
is the largest number of hops from the root to an Empty. For example, the
tree Empty has height zero; Node "x" Empty Empty has height one;
Node "x" Empty (Node "y" Empty Empty) has height two; and so on.
Sto scrivendo il mio codice in un file chiamato ch3.hs. Ecco il mio codice:
36 data Tree a = Node a (Tree a) (Tree a)
37 | Empty
38 deriving (Show)
39
40 --problem 9:Determine the height of a tree
41 height :: Tree -> Int
42 height (Tree node left right) = if (left == Empty && right == Empty) then 0 else max (height left) (height right)
apertura ghci nel terminale e digitando: ch3.hs. carico Quando faccio che ottengo il seguente errore:
Prelude> :load ch3.hs
[1 of 1] Compiling Main (ch3.hs, interpreted)
ch3.hs:42:7: Not in scope: data constructor `Tree'
Failed, modules loaded: none.
mi aspetto che il costruttore di dati Albero dovrebbe essere lì, perché ho definito nelle righe sopra il metodo di altezza. Ma quando provo a caricare il file, mi viene detto che il costruttore di dati non è in ambito. Apprezzo il tuo aiuto e la spiegazione del motivo per cui si verifica questo errore. Grazie, Kevin
Grazie, ha funzionato. Non riesco ancora a comprendere appieno cosa sta succedendo e ora sto riscontrando errori in fase di esecuzione, ma continuerò a fissare, pensare e fare il debug. –
Un altro esempio potrebbe renderlo più facile da capire. Facciamo finta che stiamo lavorando con numeri interi, non con alberi. Int è il nome del tipo intero. Non puoi aggiungere "Int + Int" perché Int è il nome del tipo, non un costruttore che restituisce un valore di quel tipo. Cose come 0, 1, 2, ... sono i costruttori, e se vuoi lavorare con i numeri interi, è così che li inserisci nel tuo programma. Applicando questo al tuo caso, 'Tree' è il nome del tipo, e' Node' (o 'Empty') è come ottieni un valore * di * quel tipo. – jrockway
HaskellWiki ha anche una sezione utile, e utilizza anche Tree come esempio: http://www.haskell.org/haskellwiki/Constructor – jrockway