[Haskell] Re: how to write a list builder? fixpoint?

Ben.Yu at combined.com Ben.Yu at combined.com
Wed Jun 2 14:38:05 EDT 2004





Thanks Oleg.

This is really a nice example. Never even dream about such wonderful use of
type classes!

It seems to me that the "fallow-undecidable-instances" for this example is
not necessary though.

It compiles with just glasgow-exts

Another question about overloading with type classes. It seems that these
overloaded functions cannot be passed as higher-order function. Is that
true?A higher order function can never be overloaded?

In an example, how can I pass "build" as a function to another function
that does some algorithm? (as the traditional Builder pattern in OO term)

myalg builder = ...... build a b c ......

Ben.



                                                                                                                                       
                      oleg at pobox.com                                                                                                   
                      Sent by:                 To:       haskell at haskell.org                                                           
                      haskell-bounces at h        cc:                                                                                     
                      askell.org               Subject:  [Haskell] Re: how to write a list builder? fixpoint?                          
                                                                                                                                       
                                                                                                                                       
                      06/01/2004 08:18                                                                                                 
                      PM                                                                                                               
                      Please respond to                                                                                                
                      oleg                                                                                                             
                                                                                                                                       





> Is it possible to write a function to build a list [a]?
> so that I can write [a,b,c,d] as "getBuilt $ build a b c d"?

Yes, in the format very close to desired.

> {-# OPTIONS -fglasgow-exts #-}
> {-# OPTIONS -fallow-undecidable-instances #-}
>
> module Foo where
>
> class BuildList a r  | r-> a where
>     build' :: [a] -> a -> r
>
> instance BuildList a [a] where
>     build' l x = reverse$ x:l
>
> instance BuildList a r => BuildList a (a->r) where
>     build' l x y = build'(x:l) y

That's it. It works both on GHC and Hugs.

*Foo> build' [] True :: [Bool]
[True]
*Foo> build' [] True False :: [Bool]
[True,False]
*Foo> build' [] True False False :: [Bool]
[True,False,False]
*Foo> build' [] 'a' 'b' 'c' 'd' 'e' :: [Char]
"abcde"
*Foo> build' [] (1::Int) :: [Int]
[1]
*Foo> build' [] (1::Int) (2::Int) :: [Int]
[1,2]
*Foo> build' [] (1::Int) (2::Int) (3::Int) :: [Int]
[1,2,3]

Note that the type annotation [Bool] etc. at the end is required: it
is the delimiter of the list. Who would have thought that the type
annotation can play the role of Nil...

_______________________________________________
Haskell mailing list
Haskell at haskell.org
http://www.haskell.org/mailman/listinfo/haskell




This message is intended only for the addressee and may contain information
that is confidential or privileged. Unauthorized use is strictly prohibited
and may be unlawful. If you are not the intended recipient, or the person
responsible for delivering to the intended recipient, you should not read,
copy, disclose or otherwise use this message, except for the purpose of
delivery to the addressee. If you have received this email in error, please
delete and advise us immediately.



More information about the Haskell mailing list