Exercitium1-0.1.0.0: Problemas de Exercitium (Volumen 1)

CopyrightExercitium (10-05-14)
LicenseGPL-3
MaintainerJoseA.Alonso@gmail.com
Safe HaskellSafe
LanguageHaskell2010

EmparejamientoDeArboles

Description

Emparejamiento de árboles

Los árboles se pueden representar mediante el siguiente tipo de datos

data Arbol a = N a [Arbol a]
  deriving Show

Por ejemplo, los árboles

  1               3
 / \             /|\ 
6   3           / | \
    |          5  4  7
    5          |     /\ 
               6    2  1

se representan por

ej1, ej2 :: Arbol Int
ej1 = N 1 [N 6 [],N 3 [N 5 []]]
ej2 = N 3 [N 5 [N 6 []], N 4 [], N 7 [N 2 [], N 1 []]]

Definir la función

emparejaArboles :: (a -> b -> c) -> Arbol a -> Arbol b -> Arbol c

tal que (emparejaArboles f a1 a2) es el árbol obtenido aplicando la función f a los elementos de los árboles a1 y a2 que se encuentran en la misma posición. Por ejemplo,

>>> emparejaArboles (+) (N 1 [N 2 [], N 3[]]) (N 1 [N 6 []])
N 2 [N 8 []]
>>> emparejaArboles (+) ej1 ej2
N 4 [N 11 [],N 7 []]
>>> emparejaArboles (+) ej1 ej1
N 2 [N 12 [],N 6 [N 10 []]]

Synopsis

Documentation

data Arbol a Source #

Tipo de árboles.

Constructors

N a [Arbol a] 

Instances

Eq a => Eq (Arbol a) Source # 

Methods

(==) :: Arbol a -> Arbol a -> Bool #

(/=) :: Arbol a -> Arbol a -> Bool #

Show a => Show (Arbol a) Source # 

Methods

showsPrec :: Int -> Arbol a -> ShowS #

show :: Arbol a -> String #

showList :: [Arbol a] -> ShowS #

ej1 :: Arbol Int Source #

Ejemplo de árbol.

ej2 :: Arbol Int Source #

Ejemplo de árbol.

emparejaArboles :: (a -> b -> c) -> Arbol a -> Arbol b -> Arbol c Source #

Definición.