[clean-list] Understanding Generics

Alexsandro Soares a_s_soares at yahoo.com.br
Fri Jul 21 17:06:56 MEST 2006


Hi all,

   I'm trying to figure out how to implement a generic
version of the function "sub". This function takes an
element and returns a list of this element and all its
subtrees. Examples:

  sub [1,2,3] = [[1,2,3], [2,3], [3], []] 
  sub (B (L 1) (L 2)) = [(B (L 1) (L 2)), (L 1), (L
2)]

  Reading the Clean manual I couldn't understand how
to use generics for this case. Can anyone help me? 

Cheers,
  Alex
   
P.S.:  The non-generic implemention would be

---- Sub.icl
------------------------------------------
module Sub
import StdGeneric, StdEnv

:: Tree a = L a | B (Tree a) (Tree a)
:: List a = Nil | Cons a (List a)

t1 = B (L 1) (L 2)
l1 = Cons 1 (Cons 2 (Cons 3 Nil))

subTree :: (Tree a) -> [(Tree a)]
subTree (L a) = [L a]
subTree (t=:(B l r)) = [t:subTree l] ++ subTree r

subList :: (List a) -> [(List a)]
subList Nil = [Nil]
subList (l=:(Cons x xs)) = [l : subList xs]

-----------------------------------------------------


	



	
		
_______________________________________________________ 
Você quer respostas para suas perguntas? Ou você sabe muito e quer compartilhar seu conhecimento? Experimente o Yahoo! Respostas !
http://br.answers.yahoo.com/


More information about the clean-list mailing list