[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 ......


                      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                                                                                                 
                      Please respond to                                                                                                

> 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]
*Foo> build' [] True False :: [Bool]
*Foo> build' [] True False False :: [Bool]
*Foo> build' [] 'a' 'b' 'c' 'd' 'e' :: [Char]
*Foo> build' [] (1::Int) :: [Int]
*Foo> build' [] (1::Int) (2::Int) :: [Int]
*Foo> build' [] (1::Int) (2::Int) (3::Int) :: [Int]

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

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