RE: [Haskell-cafe] Newbie Question on type constructors

2004-11-01 Thread Brian Beckman
Most interesting discussion -- in reading it, I realized that I had a
'hidden agenda' in asking my question (hidden even from myself), and
that is: can I put interesting functionality, like precondition checks 
data validation, in data constructors?  I suspect not, and that's why I
tend to write something like the following:

  data Shape = Circle Float
 | Square Float
deriving (Eq, Show)

  circle :: Float - Shape
  circle x = if (x = 0) then error Bad radius! else Circle x

That's fine, but I don't know how to prevent users from calling Circle
directly (in some other languages, I could declare the raw constructor
to be private, and, in Haskell, there may be some way for me to hack
module exports to hide the raw constructor, but I haven't seen a way to
do that).

In any event, it might be useful if I could have some pattern to prevent
circumvention of initialization code. Advice?

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Ben Rudiak-Gould
Sent: Monday, November 01, 2004 11:33 AM
To: [EMAIL PROTECTED]
Cc: [EMAIL PROTECTED]
Subject: Re: [Haskell-cafe] Newbie Question on type constructors

Finn Wilcox wrote:

 On Mon, 1 Nov 2004, Ben Rudiak-Gould wrote:
 
 In particular, one cannot write an invert :: (a-b) - Maybe
 (b-a) which never returns a wrong answer, except for invert = const
NothingHow about:
 
 invert = undefined
 
 This never returns an answer at all, so it can't return a wrong one!

Sorry, I should have been clearer: my Nothing return was intended to
mean I don't know an answer, not there is no answer. So my const
Nothing is like your undefined, a function which never returns an answer
(and thus never returns a wrong one).

-- Ben

___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe
___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe


RE: [Haskell-cafe] Set of reals...?

2004-10-29 Thread Brian Beckman
Very pretty, Keean, though to get it to work in Hugs Nov 2002 I had to
type the following uglier but equivalent syntax

 myInterval = Interval {
isin = (\r -
   if r == 0.6 then True else
   if r  0.7  r  1.0 then True else
   False )
} 

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Keean Schupke
Sent: Wednesday, October 27, 2004 3:53 AM
To: Stijn De Saeger
Cc: [EMAIL PROTECTED]
Subject: Re: [Haskell-cafe] Set of reals...?

I think someone else mentioned using functions earlier, rather than a
datatype why not define:

data Interval = Interval { isin :: Float - Bool }

Then each range becomes a function definition, for example:

myInterval = Interval {
   isin r
  | r == 0.6 = True
  | r  0.7  r  1.0 = True
  | otherwise = False
   }

Then you can test with:

(isin myInterval 0.6)

Keean
   



___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe
___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] NewbieQ: colon prefix for operators, e.g., Ratio ?

2004-10-10 Thread Brian Beckman
Apologies if this is the wrong mailing list in which to 
pester folks with Newbie Questions, but I couldn't find my answer after half an 
hour of scouring the Haskell Report, the Haskell Wiki, the School of _expression_ 
book and a couple of tutorials. The frustrating thing is that I know I saw 
the answerin ONE of those sources and it didn't stick at the 
time:

what does it mean when an operator is prefixed by a 
colon? For instance, in the Ratio module, the meaning of 
x % y is clear, butx :% y appears in multiple 
places and I'm confused. ___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe