Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

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


Today's Topics:

   1. Re:  Type inference question (Daniel Fischer)
   2. Re:  numerical types, the $ operator (TG)
   3. Re:  numerical types, the $ operator (Daniel Fischer)
   4. Re:  Simple laplace solver (Peter Verswyvelen)
   5. Re:  Simple laplace solver (Henk-Jan van Tuyl)
   6. Re:  numerical types, the $ operator (Zachary Turner)
   7. Re:  numerical types, the $ operator (Zachary Turner)
   8. Re:  numerical types, the $ operator (Brent Yorgey)
   9. Re:  numerical types, the $ operator (Daniel Fischer)


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

Message: 1
Date: Sun, 29 Mar 2009 18:24:04 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Type inference question
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="iso-8859-15"

Am Sonntag 29 März 2009 18:14:00 schrieb Zachary Turner:
> Given the following two function definitions:
>
> let func4 l = map (\y -> y+2) (filter (\z -> z `elem` [1..10]) (5:l))
> let func4_pf = map (+2) . filter (`elem` [1..10]) . (5 :)
>
> which are equivalent, why does the first one have a type of
>
> func4 :: (Num a, Enum a) => [a] -> [a]
>
> while the second one has a type of
>
> func4_pf :: [Integer] -> [Integer]
>
> Shouldn't the types be the same?

It's the dreaded monomorphism restriction.
Since the second is defined without arguments, it looks like a constant 
and to avoid recomputation, it must have a monomorphic type, except a 
type signature is given. That monomorphic type is defaulted to [Integer].

If you start ghci with the flag -fno-monomorphism-restriction, it will infer 
the same general type for the second function, too.


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

Message: 2
Date: Sun, 29 Mar 2009 20:00:37 +0300
From: "TG" <[email protected]>
Subject: Re: [Haskell-beginners] numerical types, the $ operator
To: "Beginners Haskell" <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"



On Sat, 28 Mar 2009 17:34 -0400, "Brent Yorgey" <[email protected]>
wrote:

> > Moreover, I've put the 'RealFrac' by looking at ":t floor".
> > What kind of class constraint whould you put for doing eg:
> > 
> > >frac x = x - fromInteger (floor (sqrt x) )
> > 
> > since 'floor' takes fractional and 'sqrt' takes RealFrac? Some kind of
> > super-class?
> 
> Every instance of RealFrac must also be an instance of Fractional, so
> just putting RealFrac would be fine.  (And I didn't have this
> memorized, I just started up ghci and typed ':info Fractional' and
> ':info RealFrac' to see how they are declared.)

Hmm, I gave the wrong types for floor and sqrt above (oops) but your
answer should still be valid as a way of looking at things
So
    floor :: (RealFrac a, Integral b) => a -> b
    sqrt :: (Floating a) => a -> a
and looking as you suggest,
    (Real a, Fractional a) => RealFrac a
    (Fractional a) => Floating a 
So in this case the answer is...
  frac :: (Floating a, RealFrac a) => a -> a
  frac x = x - fromInteger (floor (sqrt x))
No, common! Please tell me I'm wrong, that there's a simpler way!


Clear, qualified answers and a bit of showing off for having something
to aspire too. Thank you guys!
-- 
  TG
  [email protected]

-- 
http://www.fastmail.fm - Does exactly what it says on the tin



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

Message: 3
Date: Sun, 29 Mar 2009 19:25:28 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] numerical types, the $ operator
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="iso-8859-1"

Am Sonntag 29 März 2009 19:00:37 schrieb TG:
> On Sat, 28 Mar 2009 17:34 -0400, "Brent Yorgey" 
<[email protected]>
>
> wrote:
> > > Moreover, I've put the 'RealFrac' by looking at ":t floor".
> > >
> > > What kind of class constraint whould you put for doing eg:
> > > >frac x = x - fromInteger (floor (sqrt x) )
> > >
> > > since 'floor' takes fractional and 'sqrt' takes RealFrac? Some 
kind of
> > > super-class?
> >
> > Every instance of RealFrac must also be an instance of Fractional, 
so
> > just putting RealFrac would be fine.  (And I didn't have this
> > memorized, I just started up ghci and typed ':info Fractional' and
> > ':info RealFrac' to see how they are declared.)
>
> Hmm, I gave the wrong types for floor and sqrt above (oops) but 
your
> answer should still be valid as a way of looking at things
> So
>     floor :: (RealFrac a, Integral b) => a -> b
>     sqrt :: (Floating a) => a -> a
> and looking as you suggest,
>     (Real a, Fractional a) => RealFrac a
>     (Fractional a) => Floating a
> So in this case the answer is...
>   frac :: (Floating a, RealFrac a) => a -> a
>   frac x = x - fromInteger (floor (sqrt x))
> No, common! Please tell me I'm wrong, that there's a simpler way!

Slightly simpler type signature:

frac :: RealFloat a => a -> a

Prelude> :i RealFloat
class (RealFrac a, Floating a) => RealFloat a where
  floatRadix :: a -> Integer
  floatDigits :: a -> Int
  floatRange :: a -> (Int, Int)
  decodeFloat :: a -> (Integer, Int)
  encodeFloat :: Integer -> Int -> a
  exponent :: a -> Int
  significand :: a -> a
  scaleFloat :: Int -> a -> a
  isNaN :: a -> Bool
  isInfinite :: a -> Bool
  isDenormalized :: a -> Bool
  isNegativeZero :: a -> Bool
  isIEEE :: a -> Bool
  atan2 :: a -> a -> a
        -- Defined in GHC.Float
instance RealFloat Double -- Defined in GHC.Float
instance RealFloat Float -- Defined in GHC.Float


>
>
> Clear, qualified answers and a bit of showing off for having 
something
> to aspire too. Thank you guys!
> --
>   TG
>   [email protected]



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

Message: 4
Date: Sun, 29 Mar 2009 20:27:34 +0200
From: Peter Verswyvelen <[email protected]>
Subject: Re: [Haskell-beginners] Simple laplace solver
To: "Edward Z. Yang" <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

On Sat, Mar 28, 2009 at 6:09 PM, Edward Z. Yang <[email protected]> wrote:

> The list is four elements long, so I don't particularly care. Although,
> I wonder why there isn't a built-in average function.


Indeed, since we do have maximum and minimum on a list... Of course where
does it end, one could imagine ending up with a big statistics package :)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20090329/cc9cacca/attachment-0001.htm

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

Message: 5
Date: Sun, 29 Mar 2009 20:36:32 +0200
From: "Henk-Jan van Tuyl" <[email protected]>
Subject: Re: [Haskell-beginners] Simple laplace solver
To: "Peter Verswyvelen" <[email protected]>, "Edward Z. Yang"
        <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; format=flowed; delsp=yes;
        charset=iso-8859-15

On Sun, 29 Mar 2009 20:27:34 +0200, Peter Verswyvelen <[email protected]>  
wrote:

> On Sat, Mar 28, 2009 at 6:09 PM, Edward Z. Yang <[email protected]> wrote:
>
>> The list is four elements long, so I don't particularly care. Although,
>> I wonder why there isn't a built-in average function.
>
>
> Indeed, since we do have maximum and minimum on a list... Of course where
> does it end, one could imagine ending up with a big statistics package :)

And of course there are statistics packages; hstats (in Hackage) contains  
an average function, capable of handling large lists.

-- 
Met vriendelijke groet,
Henk-Jan van Tuyl


--
http://functor.bamikanarie.com
http://Van.Tuyl.eu/
--




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

Message: 6
Date: Sun, 29 Mar 2009 17:53:15 -0500
From: Zachary Turner <[email protected]>
Subject: Re: [Haskell-beginners] numerical types, the $ operator
To: John Dorsey <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

On Sat, Mar 28, 2009 at 4:53 PM, John Dorsey <[email protected]> wrote:

> > > How would an experienced guy write this without parentheses?
> >
> > I'm fairly certain it's impossible to write it without using
> > parentheses.  I would probably just write
> >
> >   x - fromInteger (floor x)
>
> Never impossible!
>
> flip subtract x . fromInteger $ floor x
> case floor x of y -> x - fromInteger y
> let y = floor x in x - fromInteger y
>

I'm a bit of a beginner myself, but I came up with this:

let (|>) x f = f x
let mapping f x = (x, f x)
let mapping2 f (x,y) = (x, f y)
let frac x = x |> mapping id |> mapping2 floor |> mapping2 fromInteger |>
uncurry (-)

A little extreme, but I still like that it illustrates the |> operator,
which is actually really useful, I borrowed the concept from F#.  I
redefined it because I actually have no idea if F# has a similar operator.
Does it?   It's obviously still easier to read the original parenthesized
version, but sometimes the |> operator really makes things very readable,
because it emphasizes the fact that you start with a single value, and send
that value through a series of transformations one after the other, and you
can read each transformation in the order that it happens, rather than with
function composition where you have to scan to the end first to see which
operation gets applied first.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20090329/62abc697/attachment-0001.htm

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

Message: 7
Date: Sun, 29 Mar 2009 18:02:14 -0500
From: Zachary Turner <[email protected]>
Subject: Re: [Haskell-beginners] numerical types, the $ operator
To: John Dorsey <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

On Sun, Mar 29, 2009 at 5:53 PM, Zachary Turner <[email protected]>wrote:

> I redefined it because I actually have no idea if F# has a similar
> operator.  Does it?


Doh, I actually meant to say I have no idea if Haskell has a similar
operator.

Zach
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20090329/c34325a0/attachment-0001.htm

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

Message: 8
Date: Sun, 29 Mar 2009 19:19:26 -0400
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] numerical types, the $ operator
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On Sun, Mar 29, 2009 at 06:02:14PM -0500, Zachary Turner wrote:
> On Sun, Mar 29, 2009 at 5:53 PM, Zachary Turner 
> <[email protected]>wrote:
> 
> > I redefined it because I actually have no idea if F# has a similar
> > operator.  Does it?
> 
> 
> Doh, I actually meant to say I have no idea if Haskell has a similar
> operator.

Not defined in the standard libraries.  There is, however, the (>>>)
operator (from Control.Arrow) which composes functions in the opposite
order.  But then you still have to put the value the functions are
applied to at the end.  I like your (|>) operator.

-Brent


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

Message: 9
Date: Mon, 30 Mar 2009 01:47:30 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] numerical types, the $ operator
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="iso-8859-15"

Am Montag 30 März 2009 00:53:15 schrieb Zachary Turner:
> On Sat, Mar 28, 2009 at 4:53 PM, John Dorsey <[email protected]> wrote:
> > > > How would an experienced guy write this without parentheses?
> > >
> > > I'm fairly certain it's impossible to write it without using
> > > parentheses.  I would probably just write
> > >
> > >   x - fromInteger (floor x)
> >
> > Never impossible!
> >
> > flip subtract x . fromInteger $ floor x
> > case floor x of y -> x - fromInteger y
> > let y = floor x in x - fromInteger y
>
> I'm a bit of a beginner myself, but I came up with this:
>
> let (|>) x f = f x
> let mapping f x = (x, f x)
> let mapping2 f (x,y) = (x, f y)
> let frac x = x |> mapping id |> mapping2 floor |> mapping2 fromInteger |>
> uncurry (-)

But John didn't use

(-) x . fromInteger . floor $ x

because it has parentheses, like your version :)
That is easily fixed, though, and since almost everything you ever need has 
already been coded by somebody else, let's use a library instead of (|>), 
mapping and mapping2:

import Control.Arrow

frac :: RealFrac a => a -> a
frac = fromInteger . floor &&& id >>> uncurry subtract

pointfree and without parentheses.

f &&& g = \x -> (f x, g x)
(for functions, it's more generally applicable to arrows), so your 
'mapping f' is 'id &&& f', your 'mapping2 f' would be 'second f', also defined 
in 
Control.Arrow.
You see that these functions are so generally useful that they already are in a 
library :)

(>>>) is forward composition (for functions, it's defined in Control.Category 
for 
more general settings), useful and readable. You can't use it to inject the 
value 
into the pipeline, though.
But often that is not necessary and pointfree style is equally readable 
(sometimes even more readable).

>
> A little extreme, but I still like that it illustrates the |> operator,
> which is actually really useful, I borrowed the concept from F#.  I
> redefined it because I actually have no idea if F# has a similar operator.
> Does it? It's obviously still easier to read the original parenthesized
> version, but sometimes the |> operator really makes things very readable,
> because it emphasizes the fact that you start with a single value, and send
> that value through a series of transformations one after the other, and you
> can read each transformation in the order that it happens, rather than with
> function composition where you have to scan to the end first to see which
> operation gets applied first.


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

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 9, Issue 39
****************************************

Reply via email to