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. Call stack (Nicolas Couture-Grenier)
2. Re: Library source (Stephen Tetley)
3. Printing the result of a function evaluation (Travis Erdman)
4. Re: Printing the result of a function evaluation
(Stephen Blackheath [to Haskell-Beginners])
5. Re: Printing the result of a function evaluation (Travis Erdman)
6. Re: Re: Printing the result of a function evaluation
(Stephen Blackheath [to Haskell-Beginners])
7. Re: Help in pattern matching (Joe Fox)
----------------------------------------------------------------------
Message: 1
Date: Mon, 8 Mar 2010 12:56:56 -0500
From: Nicolas Couture-Grenier <[email protected]>
Subject: [Haskell-beginners] Call stack
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Is there an easy way to print the call stack of the evaluation of an
expression in ghci without setting breakpoints or changing the code?
That would be very useful.
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20100308/4c20a543/attachment-0001.html
------------------------------
Message: 2
Date: Mon, 8 Mar 2010 18:05:11 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] Library source
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
On 8 March 2010 17:27, Dave Bayer <[email protected]> wrote:
>
> Why can't we agree as a community that no matter how one obtains GHC, on any
> platform by any delivery means, the default should be to provide absolutely
> every single line of source code as linked HTML? I mean, we're a serious
> language, and isn't such a principle part of the definition of being a
> serious language?
>
Haven't the 'standard' libs (aka the Hierarchical libs) they always
been here with a GHC install?
<ghc-version>/doc/html/...
Html docs for GHC, Haskell Hierarchical Libraries (with marked-up) source, Cabal
Best wishes
Stephen
------------------------------
Message: 3
Date: Mon, 8 Mar 2010 15:57:19 -0800 (PST)
From: Travis Erdman <[email protected]>
Subject: [Haskell-beginners] Printing the result of a function
evaluation
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Why oh why doesn't this work as I expect, and what is the simplest fix?
speedTest 4 = 44
time :: (Show t) => IO t -> IO t
time a = do
start <- getCPUTime
v <- a
end <- getCPUTime
let diff = (fromIntegral (end - start)) / (10^12)
print v -- WHY DOESNT THIS WORK? ALWAYS PRINTS () ....
printf "Computation time: %0.3f sec\n" (diff :: Double)
return v
main :: IO ()
main = do
args <- getArgs
time $ speedTest (read $ head args) `seq` return ()
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20100308/c83ea593/attachment-0001.html
------------------------------
Message: 4
Date: Tue, 09 Mar 2010 13:16:49 +1300
From: "Stephen Blackheath [to Haskell-Beginners]"
<[email protected]>
Subject: Re: [Haskell-beginners] Printing the result of a function
evaluation
To: Travis Erdman <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8
Travis,
because 'speedTest (read $ head args) `seq` return ()' is semantically
equivalent to 'return ()' whose type is IO () therefore 'v <- a' assigns
() to v.
'time (return $! speedTest (read $ head args))' is better...
however, it's still not going to do what you want (time how long it
takes speedTest to evaluate, I'm assuming), because (speedTest (read $
head args)) will only get evaluated once.
The only way to actually time how long speedTest takes is to pass a
function e.g. (Int -> IO t) to 'time', and have 'time' pass it a dummy
argument, so that GHC can't optimize the evaluation of speedTest out.
Steve
Travis Erdman wrote:
> Why oh why doesn't this work as I expect, and what is the simplest fix?
>
> speedTest 4 = 44
>
> time :: (Show t) => IO t -> IO t
> time a = do
> start <- getCPUTime
> v <- a
> end <- getCPUTime
> let diff = (fromIntegral (end - start)) / (10^12)
> print v -- WHY DOESNT THIS WORK? ALWAYS PRINTS () ....
> printf "Computation time: %0.3f sec\n" (diff :: Double)
> return v
>
> main :: IO ()
> main = do
> args <- getArgs
> time $ speedTest (read $ head args) `seq` return ()
>
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
------------------------------
Message: 5
Date: Mon, 8 Mar 2010 16:55:43 -0800 (PST)
From: Travis Erdman <[email protected]>
Subject: [Haskell-beginners] Re: Printing the result of a function
evaluation
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Steve, I should have provided the following details ...
The code I pasted was a timing function I've come across in various Haskell
literature, and it "works" as is ... at least, the timing results it prints are
plausible.
My sole "contribution" to the code was the "print v" line, in an attempt to
have it print BOTH the result of the function evaluation AND the time it took
to evaluate it (and still only evaluating the function once).
As noted, my "print v" prints only "()" regardless of the function -- not what
I wanted. But the timing part still works. There has got to be an easy fix
here, but I have yet to discover it ...
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20100308/6a5cb750/attachment-0001.html
------------------------------
Message: 6
Date: Tue, 09 Mar 2010 15:02:37 +1300
From: "Stephen Blackheath [to Haskell-Beginners]"
<[email protected]>
Subject: Re: [Haskell-beginners] Re: Printing the result of a function
evaluation
To: Travis Erdman <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8
Travis,
OK - I'm with ya. Well, 'speedTest (read $ head args) `seq` return ()'
will evaluate your expression but it'll return (). The quick fix would
be to use 'return $! speedTest (read $ head args)' - That will also
evaluate the expression, but it'll return the output too. The $! is the
bit that forces it the evaluation of the function (so the evaluation
happens during the bit that is timed).
Actions are evaluated just before they're sequenced in IO, and this is
actually the source of all forcing of evaluation in Haskell (or at
least, in GHC). Doing 'x `seq` return ()' is one way to tie the
evaluation to x to IO's evaluation, and 'return $! x' is another way. I
think the second one is equivalent to 'x `seq` return x`. Or, in your
case it would be 'let x = speedTest (read $ head args) in x `seq` return
x', so $! saves you a bit of typing.
1. IO says 'evaluate the IO a value!' which requires the argument to be
passed to the 'return' function (but not evaluated).
2. $! says 'force evaluation of the argument, then pass it to the
function. This causes the evaluation to be tied to IO, and therefore
actually forces the evaluation.
Clear as coffee?
Steve
Travis Erdman wrote:
> Steve, I should have provided the following details ...
>
> The code I pasted was a timing function I've come across in various
> Haskell literature, and it "works" as is ... at least, the timing
> results it prints are plausible.
>
> My sole "contribution" to the code was the "print v" line, in an attempt
> to have it print BOTH the result of the function evaluation AND the time
> it took to evaluate it (and still only evaluating the function once).
>
> As noted, my "print v" prints only "()" regardless of the function --
> not what I wanted. But the timing part still works. There has got to
> be an easy fix here, but I have yet to discover it ...
>
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
------------------------------
Message: 7
Date: Tue, 9 Mar 2010 10:04:18 +0530
From: Joe Fox <[email protected]>
Subject: Re: [Haskell-beginners] Help in pattern matching
To: Stephen Tetley <[email protected]>
Cc: beginners <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Thanks Stepen
will try out factoring
On Mon, Mar 8, 2010 at 7:44 PM, Stephen Tetley <[email protected]>wrote:
> Hi Joe - I've cc'ed back to list...
>
> I'm supposing from what you've said that only DataConst1 & DataConst2
> have ids - so I've made a type synonym VId
>
>
> type VId = Int
>
> data TestData = DataConst1 VId String
> | DataConst2 VId Int
> | DataConst3 Sting String
> | DataConst4 String Int
>
>
> If both DataConst1 & DataConst2 have VId's the you right - you have to
> pattern match for both:
>
> setVid_to_4 :: TestData -> TestData
> setVid_to_4 (DataConst1 _ y) = DataConst1 4 y
> setVid_to_4 (DataConst2 _ y) = DataConst2 4 y
> setVid_to_4 var = var
>
>
> If all the cases of TestData had a VId e.g.:
>
>
> data TestData = DataConst1 VId String
> | DataConst2 VId Int
> | DataConst3 VId Sting String
> | DataConst4 Vid String Int
>
> ... then you could factor out VId to get these two data types
>
>
> data FactoredTD = FactoredTD VId TD
>
> data TD = DC1 String
> | DC2 Int
> | DC3 String String
> | DC4 String Int
>
> setVid_to_4 :: FactoredTD -> FactoredTD
> setVid_to_4 (FactoredTD _ y) = FactoredTD 4 y
>
>
> This factoring transformation is quite common.
>
> For your particular data type you could factor the other way:
>
>
> data TestDataX = DataConst1_or_2 VId (Either String Int)
> | DataConst3 Sting String
> | DataConst4 String Int
>
>
> setVid_to_4 :: TestDataX -> TestDataX
> setVid_to_4 (DataConst1_or_2 _ y) = DataConst1_or_2 4 y
> setVid_to_4 y = y
>
> However this factoring is quite horrible - I can't think of anywhere
> where I've seen it.
>
> Generally I wouldn't be too concerned about redundancy in pattern
> matching. If its easy to consistently name all the constructors in an
> algebraic data type, then that's a strong indication that you've put
> it into a good form.
>
> If you're still taxed by duplicate pattern matching you can then write
> projection and modification functions to do it once and once only:
>
>
> -- Has to return a Maybe as DC3 and DC4 have no ID
> getVId :: TestData -> Maybe VId
> getVid (DataConst1 iden _) = Just iden
> getVid (DataConst2 iden _) = Just iden
> getVid _ = Nothing
>
>
> -- note - uses an update function that accesses the original value
> -- rather than a simple replace
> --
> updateVid :: (Vid -> Vid) -> TestData -> TestData
> updateVid f (DataConst1 iden y) = DataConst1 (f iden) y
> updateVid f (DataConst2 iden y) = DataConst2 (f iden) y
> updateVid f var = var
>
> incrementVid :: TestData -> TestData
> incrementVid = updateVid (\x -> x+1)
>
> Best wishes
>
> Stephen
>
> On 8 March 2010 12:19, Joe Fox <[email protected]> wrote:
> > Hi Guys,
> >
> > First thanks for all yours inputs.
> >
> > Actually test is just a simple function , actually my actual function is
> > pretty complex and huge ...
> >
> > let me redefine Test function , i guess i took a bad example which
> created
> > lot of confusion
> >
> > the type of the test functions will be something like this , (i don't
> have
> > any ambiguity on type of the function, )
> >
> > test:: TestData -> TestData
> >
> > My question is like is there any way i can match multiple pattern in a
> > function statement .
> >
> > like this (I know this doesnt work , but just trying to explain whats on
> my
> > mind)
> >
> >
> > test var@(DataConst1 x y) == var@(DataConst2 x y) = var{id=4} -- let's
> say
> > id is the record holder for first record
> > test var@(_) = var --- any for other two its returns the same
> >
> > the working code would be like
> > test var@(DataConst1 x y) = var{id=4}
> > test var@(DataConst2 x y) = var{id=4} --- I want these two lines in one
> > line, because the logic is same
> > test var@(_) = var
> >
> > some thing of this sort.
> > I hope i didnt add to the confusion
> >
> >
> > Thanks
> > Joe
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20100308/d01ae2c5/attachment.html
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 21, Issue 8
****************************************