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: homogeneous value list (Brent Yorgey)
2. Re: homogeneous value list (Brent Yorgey)
3. Time interval calculation (legajid)
4. Re: Time interval calculation (legajid)
5. Re: Time interval calculation (Daniel Fischer)
6. Re: Time interval calculation (Daniel Fischer)
7. Re: Time interval calculation (legajid)
8. Re: Time interval calculation (Daniel Fischer)
9. Re: Time interval calculation (legajid)
----------------------------------------------------------------------
Message: 1
Date: Sat, 13 Mar 2010 15:06:44 -0500
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] homogeneous value list
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=iso-8859-1
On Fri, Mar 12, 2010 at 09:44:18AM -0500, Ron Leisti wrote:
> Does this have any implications say, for using infinite data
> structures? When we can we assume laziness (in order to use
> abstractions like infinity), and when can we not?
Not really. Personally, I find this thread a bit pedantic; at some
point it's good to understand the finer points of the Haskell
standard, but this really has almost no bearing on beginners. Use
lazy, infinite data structures to your heart's content. =)
-Brent
>
> On Fri, Mar 12, 2010 at 9:39 AM, Thomas Davie <[email protected]> wrote:
> >
> > On 12 Mar 2010, at 14:33, Ozgur Akgun wrote:
> >
> >
> > On 12 March 2010 14:28, Thomas Davie <[email protected]> wrote:
> >>
> >> On 12 Mar 2010, at 14:25, Salil Wadnerkar wrote:
> >>
> >> On Fri, Mar 12, 2010 at 10:21 PM, Ozgur Akgun <[email protected]>
> >> wrote:
> >>
> >> Another boring variant from me then.
> >>
> >> isHomogeneous xs = all (first==) xs
> >>
> >> where first = head xs
> >>
> >>
> >> Shouldn't
> >> head xs
> >> give an exception on an empty list?
> >>
> >> An error, and only if it's evaluated. Lazy evaluation means it's not
> >> evaluated here.
> >> Of course another non-strict algorithm *might* evaluate head xs here, so
> >> this version won't work in all possible Haskell implementations, only the
> >> current ones which use lazy evaluation.
> >> Bob
> >
> > So we need to make sure our code would work in a strict environment, even
> > though we are using a lazily evaluated language, just in case if someone
> > comes up with a not-that-lazy implementation of haskell?
> >
> > If I was thinking this way, I would just stop using haskell at all.
> >
> > Not quite, you need to make sure your code works in *all* non-strict
> > evaluation models. Lazy evaluation is not the only non-strict evaluation
> > method, another implementation of non-strict semantics might actually
> > evaluate head xs.
> > Bob
> > _______________________________________________
> > Beginners mailing list
> > [email protected]
> > http://www.haskell.org/mailman/listinfo/beginners
> >
> >
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
------------------------------
Message: 2
Date: Sat, 13 Mar 2010 15:08:26 -0500
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] homogeneous value list
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=iso-8859-1
On Fri, Mar 12, 2010 at 05:15:24PM +0100, Daniel Fischer wrote:
> Am Freitag 12 März 2010 15:40:36 schrieb Brent Yorgey:
> > On Fri, Mar 12, 2010 at 03:31:54PM +0100, Daniel Fischer wrote:
> > > Am Freitag 12 März 2010 15:25:48 schrieb Salil Wadnerkar:
> > > > On Fri, Mar 12, 2010 at 10:21 PM, Ozgur Akgun <[email protected]>
> > >
> > > wrote:
> > > > > Another boring variant from me then.
> > > > >
> > > > > isHomogeneous xs = all (first==) xs
> > > > > where first = head xs
> > > >
> > > > Shouldn't
> > > > head xs
> > > > give an exception on an empty list?
> > >
> > > Only if it's demanded, which it isn't here, since all doesn't look at
> > > the condition when facing an empty list.
> > >
> > > But it's good practice to never call head on a list which may be
> > > empty, so I wouldn't like Ozgur's variant in real code. It's great for
> > > golf, though
> > >
> > > :D
> >
> > I would argue that it's good practice simply to never call head at
> > all. I stopped a while ago, and I haven't missed it. =)
> >
> > -Brent
>
> I would argue that in
>
> snub = map head . group . sort
>
> and the like, calling head is perfectly cromulent. But apart from that, I
> don't remember using head in the last years.
Ah, true, I guess that's the one place I've used head as well. =)
-Brent
------------------------------
Message: 3
Date: Sat, 13 Mar 2010 21:20:58 +0100
From: legajid <[email protected]>
Subject: [Haskell-beginners] Time interval calculation
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Hi,
my following code should show time before executing listeprem, then time
after execution.
import System.Time
gettime :: IO ClockTime
gettime = getClockTime
main=do
t1d <- gettime
let t1=last (listeprem 5000)
t1f <- gettime
putStrLn ("Methode 1 : " ++ show t1d)
putStrLn (" " ++ show t1)
putStrLn (" " ++ show t1f)
Looking at the screen, t1d is displayed then, after a few seconds, t1
and t1f.
But, t1d and t1f are equal.
It seems like if t1d and t1f where calculated at start of procedure,
before we need calculating t1 for putStrLn.
How can i have t1f evaluated after t1, so i can calculate time elapsed
for calculation of t1?
Thanks,
Didier
------------------------------
Message: 4
Date: Sat, 13 Mar 2010 21:42:12 +0100
From: legajid <[email protected]>
Subject: Re: [Haskell-beginners] Time interval calculation
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
After a few efforts, i've understood my mistake.
I now write :
t1d <- gettime
putStrLn ("Methode 1 : " ++ show t1d)
let t1=last (listeprem 5000)
putStrLn (" " ++ show t1)
t1f <- gettime
putStrLn (" " ++ show t1f)
and that's ok.
What i feel shocking is having io code mixed within calculation code.
I think it's possible to force calculation of t1d, t1 and t1f before
(or without) displaying their values (i just want t1f - t1d).
But how to make it ?
Thanks,
Didier
.
legajid a écrit :
> Hi,
> my following code should show time before executing listeprem, then
> time after execution.
>
> import System.Time
> gettime :: IO ClockTime
> gettime = getClockTime
>
> main=do
> t1d <- gettime
> let t1=last (listeprem 5000)
> t1f <- gettime
> putStrLn ("Methode 1 : " ++ show t1d)
> putStrLn (" " ++ show t1)
> putStrLn (" " ++ show t1f)
>
> Looking at the screen, t1d is displayed then, after a few seconds, t1
> and t1f.
> But, t1d and t1f are equal.
> It seems like if t1d and t1f where calculated at start of procedure,
> before we need calculating t1 for putStrLn.
> How can i have t1f evaluated after t1, so i can calculate time elapsed
> for calculation of t1?
>
> Thanks,
> Didier
>
>
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
------------------------------
Message: 5
Date: Sat, 13 Mar 2010 21:34:41 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Time interval calculation
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Am Samstag 13 März 2010 21:20:58 schrieb legajid:
> Hi,
> my following code should show time before executing listeprem, then time
> after execution.
>
> import System.Time
> gettime :: IO ClockTime
> gettime = getClockTime
>
> main=do
> t1d <- gettime
This gets the time, now.
> let t1=last (listeprem 5000)
This doesn't do the calculation, it only creates a thunk what to calculate
should t1 be demanded, takes a few nanoseconds
> t1f <- gettime
This gets the time, a few nanoseconds later
> putStrLn ("Methode 1 : " ++ show t1d)
> putStrLn (" " ++ show t1)
Now t1 is demanded, and only now it's calculated.
> putStrLn (" " ++ show t1f)
>
> Looking at the screen, t1d is displayed then, after a few seconds, t1
> and t1f.
> But, t1d and t1f are equal.
> It seems like if t1d and t1f where calculated at start of procedure,
> before we need calculating t1 for putStrLn.
Exactly.
> How can i have t1f evaluated after t1, so i can calculate time elapsed
> for calculation of t1?
Depends on what sort of value t1 is. If it's a simple value like an Integer
(as I think it is here, derniere nombre premier avant 5000, 5000-th
prime?),
{-# LANGUAGE BangPatterns #-}
...
let !t1 = last (listeprem 5000)
t1f <- gettime
or
let t1 = last (listeprem 5000)
t1f <- t1 `seq` gettime
will do. For small values, printing takes negligible time, so
t1d <- gettime
putStrLn (" " ++ show t1f)
t1f <- gettime
is another option. But getClockTime isn't particularly exact, I'd suggest
using System.CPUTime.getCPUTime
>
> Thanks,
> Didier
------------------------------
Message: 6
Date: Sat, 13 Mar 2010 21:50:19 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Time interval calculation
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Am Samstag 13 März 2010 21:42:12 schrieb legajid:
> After a few efforts, i've understood my mistake.
> I now write :
> t1d <- gettime
> putStrLn ("Methode 1 : " ++ show t1d)
> let t1=last (listeprem 5000)
> putStrLn (" " ++ show t1)
> t1f <- gettime
> putStrLn (" " ++ show t1f)
>
> and that's ok.
>
> What i feel shocking is having io code mixed within calculation code.
Right, that's not so good.
> I think it's possible to force calculation of t1d, t1 and t1f before
> (or without) displaying their values (i just want t1f - t1d).
> But how to make it ?
Depends on the types. Often a bang-pattern or a `seq` is enough, for other
types, you might want to look at Control.DeepSeq and
Control.Parallel.Strategies. NFData, rnf, deepseq, using, demanding, ...
may help you. For lists, sometimes (length list) `seq` whatever is enough,
without needing strategies.
>
> Thanks,
> Didier
------------------------------
Message: 7
Date: Sat, 13 Mar 2010 22:02:41 +0100
From: legajid <[email protected]>
Subject: Re: [Haskell-beginners] Time interval calculation
To: Daniel Fischer <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Sorry Daniel,
i've seen your answer just after posting my reply.
But i can't write !t1f <- gettime, nor t1f <- !gettime; because it's an IO ?
Your method is a good help form me.
Thanks,
Didier
Daniel Fischer a écrit :
> Am Samstag 13 März 2010 21:20:58 schrieb legajid:
>
>> Hi,
>> my following code should show time before executing listeprem, then time
>> after execution.
>>
>> import System.Time
>> gettime :: IO ClockTime
>> gettime = getClockTime
>>
>> main=do
>> t1d <- gettime
>>
>
> This gets the time, now.
>
>
>> let t1=last (listeprem 5000)
>>
>
> This doesn't do the calculation, it only creates a thunk what to calculate
> should t1 be demanded, takes a few nanoseconds
>
>
>> t1f <- gettime
>>
>
> This gets the time, a few nanoseconds later
>
>
>> putStrLn ("Methode 1 : " ++ show t1d)
>> putStrLn (" " ++ show t1)
>>
>
> Now t1 is demanded, and only now it's calculated.
>
>
>> putStrLn (" " ++ show t1f)
>>
>> Looking at the screen, t1d is displayed then, after a few seconds, t1
>> and t1f.
>> But, t1d and t1f are equal.
>> It seems like if t1d and t1f where calculated at start of procedure,
>> before we need calculating t1 for putStrLn.
>>
>
> Exactly.
>
>
>> How can i have t1f evaluated after t1, so i can calculate time elapsed
>> for calculation of t1?
>>
>
> Depends on what sort of value t1 is. If it's a simple value like an Integer
> (as I think it is here, derniere nombre premier avant 5000, 5000-th
> prime?),
>
> {-# LANGUAGE BangPatterns #-}
> ...
> let !t1 = last (listeprem 5000)
> t1f <- gettime
>
> or
>
> let t1 = last (listeprem 5000)
> t1f <- t1 `seq` gettime
>
> will do. For small values, printing takes negligible time, so
>
> t1d <- gettime
> putStrLn (" " ++ show t1f)
> t1f <- gettime
>
> is another option. But getClockTime isn't particularly exact, I'd suggest
> using System.CPUTime.getCPUTime
>
>
>> Thanks,
>> Didier
>>
>
>
------------------------------
Message: 8
Date: Sat, 13 Mar 2010 22:24:09 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Time interval calculation
To: legajid <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Am Samstag 13 März 2010 22:02:41 schrieb legajid:
> Sorry Daniel,
> i've seen your answer just after posting my reply.
Thought so. Happens to me often enough :)
> But i can't write !t1f <- gettime,
You can, and it's much like
let !t1 = ...
for the pure calculation, it evaluates t1f to the outermost constructor. If
deeper evaluation is needed, like for pure values, you have to use the
appropriate forcing strategy.
!a <- mx
is less important for IO than for other monads, though, since IO imposes a
fair amount of sequencing. In particular, the sequencing of IO makes
!t1f <- gettime
superfluous, since all that could be delayed in gettime is the
transformation of the timestamp retrieved from the system clock to the
ClockTime datatype, the timestamp is retrieved at the exact point in the
IO-sequence.
> nor t1f <- !gettime;
I'm not sure whether you can write that, but it wouldn't be what you want
anyway. If that's legal BangPattern syntax, it says "evaluate gettime to
the outermost constructor", which is far less than we need to evaluate
gettime to bind its result to a name.
> because it's an
> IO ? Your method is a good help form me.
>
> Thanks,
> Didier
------------------------------
Message: 9
Date: Sat, 13 Mar 2010 23:09:48 +0100
From: legajid <[email protected]>
Subject: Re: [Haskell-beginners] Time interval calculation
To: Daniel Fischer <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
I tried
!t1f <- gettime
and i got an error message :
premier.hs:92:1: Illegal bang-pattern (use -XBangPatterns)
Failed, modules loaded: none.
t1f <- !gettime gives :
premier.hs:92:8: parse error on input `!'
Failed, modules loaded: none.
Daniel Fischer a écrit :
> Am Samstag 13 März 2010 22:02:41 schrieb legajid:
>
>> Sorry Daniel,
>> i've seen your answer just after posting my reply.
>>
>
> Thought so. Happens to me often enough :)
>
>
>> But i can't write !t1f <- gettime,
>>
>
> You can, and it's much like
>
> let !t1 = ...
>
> for the pure calculation, it evaluates t1f to the outermost constructor. If
> deeper evaluation is needed, like for pure values, you have to use the
> appropriate forcing strategy.
>
> !a <- mx
>
> is less important for IO than for other monads, though, since IO imposes a
> fair amount of sequencing. In particular, the sequencing of IO makes
>
> !t1f <- gettime
>
> superfluous, since all that could be delayed in gettime is the
> transformation of the timestamp retrieved from the system clock to the
> ClockTime datatype, the timestamp is retrieved at the exact point in the
> IO-sequence.
>
>
>> nor t1f <- !gettime;
>>
>
> I'm not sure whether you can write that, but it wouldn't be what you want
> anyway. If that's legal BangPattern syntax, it says "evaluate gettime to
> the outermost constructor", which is far less than we need to evaluate
> gettime to bind its result to a name.
>
>
>> because it's an
>> IO ? Your method is a good help form me.
>>
>> Thanks,
>> Didier
>>
>
>
>
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 21, Issue 14
*****************************************