Send Beginners mailing list submissions to
        beginners@haskell.org

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  Ratio data constructor (Galaxy Being)
   2. Re:  Ratio data constructor (Galaxy Being)
   3. Re:  Ratio data constructor (Bob Ippolito)
   4. Re:  Ratio data constructor (David McBride)


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

Message: 1
Date: Tue, 20 Jul 2021 15:42:45 -0500
From: Galaxy Being <borg...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: [Haskell-beginners] Ratio data constructor
Message-ID:
        <cafahfsuc8t-4rtyag2zp70xkffitahu7a7ou3nant2mtm_o...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

I'm investigating rational numbers with Haskell. This is the source I've
found

data Ratio a = !a :% !a deriving (Eq)

reduce ::  (Integral a) => a -> a -> Ratio a
{-# SPECIALISE reduce :: Integer -> Integer -> Rational #-}
reduce _ 0              =  ratioZeroDenominatorError
reduce x y              =  (x `quot` d) :% (y `quot` d)
                           where d = gcd x y
(%) :: (Integral a) => a -> a -> Ratio a
x % y =  reduce (x * signum y) (abs y)

The Ratio data type would seem to be a parameterized type with two
parameters of the same type that must be "settled" in that they're not to
be lazily dealt with. Then the :% is the data constructor, the : meaning
it's a data constructor and not just an operation function. So this could
have been

data Ratio a = :% !a !a deriving (Eq)

correct? But then what confuses me is in reduce, why

reduce x y  =  (x `quot` d) :% (y `quot` d)

and not just %? We have :% defined in the data type and then (%) defined as
a function. What is going on here?

-- 
⨽
Lawrence Bottorff
Grand Marais, MN, USA
borg...@gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20210720/2f41e40d/attachment-0001.html>

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

Message: 2
Date: Tue, 20 Jul 2021 16:16:55 -0500
From: Galaxy Being <borg...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Ratio data constructor
Message-ID:
        <cafahfsws68cqch1n4j4rojnqr5vxv2wq8_njyknd3yfhbvc...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

... does the last question have to do with a "smart constructor" by chance?
If so, how?

On Tue, Jul 20, 2021 at 3:42 PM Galaxy Being <borg...@gmail.com> wrote:

> I'm investigating rational numbers with Haskell. This is the source I've
> found
>
> data Ratio a = !a :% !a deriving (Eq)
>
> reduce ::  (Integral a) => a -> a -> Ratio a
> {-# SPECIALISE reduce :: Integer -> Integer -> Rational #-}
> reduce _ 0              =  ratioZeroDenominatorError
> reduce x y              =  (x `quot` d) :% (y `quot` d)
>                            where d = gcd x y
> (%) :: (Integral a) => a -> a -> Ratio a
> x % y =  reduce (x * signum y) (abs y)
>
> The Ratio data type would seem to be a parameterized type with two
> parameters of the same type that must be "settled" in that they're not to
> be lazily dealt with. Then the :% is the data constructor, the : meaning
> it's a data constructor and not just an operation function. So this could
> have been
>
> data Ratio a = :% !a !a deriving (Eq)
>
> correct? But then what confuses me is in reduce, why
>
> reduce x y  =  (x `quot` d) :% (y `quot` d)
>
> and not just %? We have :% defined in the data type and then (%) defined
> as a function. What is going on here?
>
> --
> ⨽
> Lawrence Bottorff
> Grand Marais, MN, USA
> borg...@gmail.com
>


-- 
⨽
Lawrence Bottorff
Grand Marais, MN, USA
borg...@gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20210720/dcb3378d/attachment-0001.html>

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

Message: 3
Date: Tue, 20 Jul 2021 14:16:59 -0700
From: Bob Ippolito <b...@redivi.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Ratio data constructor
Message-ID:
        <cacwmpm-uaeu+2rqs-xwc6__dprcj8w2o1hew0xubmhbfj5p...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

On Tue, Jul 20, 2021 at 1:43 PM Galaxy Being <borg...@gmail.com> wrote:

> I'm investigating rational numbers with Haskell. This is the source I've
> found
>
> data Ratio a = !a :% !a deriving (Eq)
>
> reduce ::  (Integral a) => a -> a -> Ratio a
> {-# SPECIALISE reduce :: Integer -> Integer -> Rational #-}
> reduce _ 0              =  ratioZeroDenominatorError
> reduce x y              =  (x `quot` d) :% (y `quot` d)
>                            where d = gcd x y
> (%) :: (Integral a) => a -> a -> Ratio a
> x % y =  reduce (x * signum y) (abs y)
>
> The Ratio data type would seem to be a parameterized type with two
> parameters of the same type that must be "settled" in that they're not to
> be lazily dealt with. Then the :% is the data constructor, the : meaning
> it's a data constructor and not just an operation function. So this could
> have been
>

The type has one parameter a, the (:%) constructor has two arguments (both
of type a).


> data Ratio a = :% !a !a deriving (Eq)
>

You always need to use parentheses around an infix operator to use it in
prefix, just like you need backticks around a function to use it as infix
(like `quot` in the implementation of reduce).

data Ratio a = (:%) !a !a deriving (Eq)


> correct? But then what confuses me is in reduce, why
>
> reduce x y  =  (x `quot` d) :% (y `quot` d)
>
> and not just %? We have :% defined in the data type and then (%) defined
> as a function. What is going on here?
>

The function (%) uses reduce in its definition, the equation would never
terminate due to infinite recursion if the function (%) was used instead of
the constructor (:%).

-bob
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20210720/2020919d/attachment-0001.html>

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

Message: 4
Date: Tue, 20 Jul 2021 17:26:15 -0400
From: David McBride <toa...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Ratio data constructor
Message-ID:
        <can+tr43vpq3azayypuahizqx_h2xtkqnyuhdreg+jhqrsme...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

The Ratio type is a fraction. If a is Integer, it is an infinite precision
fraction. You can divide it arbitrarily and it will never lose any
precision (though it will take more space).

Reduce is designed to create a fraction from two numbers, not solve it. so
if you supply 5 and 10, it will return a Ratio 1/2, because 5 is the
greatest common denominator.

The % operator appears to take arbitrary numbers but it seems designed to
intend to keep the negative sign on the numerator, so as to maintain some
invariant in the data.

On Tue, Jul 20, 2021 at 4:43 PM Galaxy Being <borg...@gmail.com> wrote:

> I'm investigating rational numbers with Haskell. This is the source I've
> found
>
> data Ratio a = !a :% !a deriving (Eq)
>
> reduce ::  (Integral a) => a -> a -> Ratio a
> {-# SPECIALISE reduce :: Integer -> Integer -> Rational #-}
> reduce _ 0              =  ratioZeroDenominatorError
> reduce x y              =  (x `quot` d) :% (y `quot` d)
>                            where d = gcd x y
> (%) :: (Integral a) => a -> a -> Ratio a
> x % y =  reduce (x * signum y) (abs y)
>
> The Ratio data type would seem to be a parameterized type with two
> parameters of the same type that must be "settled" in that they're not to
> be lazily dealt with. Then the :% is the data constructor, the : meaning
> it's a data constructor and not just an operation function. So this could
> have been
>
> data Ratio a = :% !a !a deriving (Eq)
>
> correct? But then what confuses me is in reduce, why
>
> reduce x y  =  (x `quot` d) :% (y `quot` d)
>
> and not just %? We have :% defined in the data type and then (%) defined
> as a function. What is going on here?
>
> --
> ⨽
> Lawrence Bottorff
> Grand Marais, MN, USA
> borg...@gmail.com
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20210720/2a4ebf73/attachment.html>

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

Subject: Digest Footer

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


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

End of Beginners Digest, Vol 156, Issue 4
*****************************************

Reply via email to