ho scritto il seguente codice per la gestione di un albero binario polimorfico in Haskell come preparazione per l'esame di programmazione funzionale la prossima settimana:Haskell polimorfico Albero Somma
data ITree t = Leaf | Node t (ITree t) (ITree t)
deriving (Eq, Ord, Show)
treeSum :: ITree t -> Int
treeSum Leaf = 0
treeSum (Node n t1 t2) = n + (treeSum t1) + (treeSum t2)
Ora ho il problema, che il codice non lo fa compile:
...\tree.hs:8:26:
Couldn't match type `t' with `Int'
`t' is a rigid type variable bound by
the type signature for treeSum :: ITree t -> Int
at ...\tree.hs:7:1
In the first argument of `(+)', namely `n'
In the first argument of `(+)', namely `n + (treeSum t1)'
In the expression: n + (treeSum t1) + (treeSum t2)
Failed, modules loaded: none.
Prelude>
Sai cosa c'è di sbagliato in treeSum? Penso che abbia qualcosa a che fare con il tipo polimorfo di ITree, ma non so come risolverlo. Devo specificare che il tipo t deve essere un tipo che può essere contato/enumerato? Probabilmente con un'istanza di classe di una classe di questo tipo?
Grazie in anticipo per il vostro aiuto!
Simon
Cosa dovrebbe 'treeSum (Node" marshmellow "Leaf Leaf)' essere? – dave4420
Non ce n'è bisogno :) Ma per Floats! – saimn
Giusto, ma il tipo che dai per 'treeSum' promette che funziona per qualsiasi tipo' t', anche 'String'. – dave4420