[Haskell-cafe] Newbie Question on type constructors

Keean Schupke k.schupke at imperial.ac.uk
Mon Nov 1 17:45:10 EST 2004


Just wanted to point out you can get accessor/deconstructor functions
using record notation:

    data Shape = Circle { radius :: Double } | Square { length :: Double }

Keean.

Benjamin Franksen wrote:

>On Monday 01 November 2004 22:34, you wrote:
>  
>
>>On 2004-11-01 at 23:01+0100 Benjamin Franksen wrote:
>>    
>>
>>>On Monday 01 November 2004 21:51, Jon Fairbairn wrote:
>>>      
>>>
>>>>Put the data declaration in a module, export the type, but
>>>>
>>>>not the constructor you want to hide:
>>>>        
>>>>
>>>>>module Shape (Shape(Square), circle) where
>>>>>          
>>>>>
>>>Since we were talking about 'what can one do with a constructor as
>>>compared with a function', it should be mentioned that this also has a
>>>disadvantage: you loose pattern matching outside the module. I.e. no
>>>longer can say
>>>
>>>	...
>>>	case x of
>>>		Circle radius -> ...
>>>		Square edge -> ...
>>>
>>>BTW, would it be possible (in principle) to allow constructor export
>>>'read-only', so matching against a constructor is allowed but not
>>>constructing a value?
>>>      
>>>
>>I don't see why not. To add something to the debate about
>>what Circle /is/, I'd like to mention that the declaration
>>
>>   data Shape = Circle Double | Square Double
>>
>>is a convenience that declares three new names (Shape,
>>Circle and Square), but five entities.
>>
>>There's Shape: a type, Circle, Square:: Double -> Shape:
>>constructor functions, and Circle, Square:: Shape -> (Double
>>-> t) -> t -> t: destructor functions.
>>    
>>
>
>Interesting point of view! But... could you explain the types of the 
>destructors? And how one would use them to deconstruct a Shape? Because, 
>hmmm, isn't it rather *one* destructor with type
>
>	destructShape :: Shape -> (Double -> t) -> (Double -> t) -> t
>
>where the second and third arguments explain what to do with a Circle resp. a 
>Square? So that
>
>	case s of
>		Circle r -> f r
>		Square l -> g l
>
>is another way to write
>
>	destructShape s g f
>
>Ben
>_______________________________________________
>Haskell-Cafe mailing list
>Haskell-Cafe at haskell.org
>http://www.haskell.org/mailman/listinfo/haskell-cafe
>  
>



More information about the Haskell-Cafe mailing list