[Haskell-beginners] Profiling haskell code

Brent Yorgey byorgey at seas.upenn.edu
Mon Dec 8 12:21:47 EST 2008


> --function "number" which generates an array of numbers, it takes the
> ends of the range for numbers as inputs
> 
> number s e = if s > e
>               then []
>               else s : number (s + 1) e

Just an aside: instead of 'number s e' you can just write [s..e] .
 
> Now I want the array of numbers generated by the first function "number"
> tobe the input of the second function"quicksort".
> Then how should I apply the function number to quicksort?

Like this:  quicksort (number 3 50)

You always apply a function f to an input x like this: f x .  So the
above code says to apply quicksort to the input (number 3 50), which
is of course the output from applying number to the inputs 3 and 50.
The parentheses are needed since without them, 'quicksort number 3 50'
tries to give three inputs to quicksort, namely 'number' '3' and '50',
which is obviously wrong.

> Also do tel me which is the book that I can refer to for Haskell? 

There are many available books and online tutorials.  Some ones I might recommend for you:

  The Haskell wikibook -- http://en.wikibooks.org/wiki/Haskell 
  Yet Another Haskell Tutorial -- http://en.wikibooks.org/wiki/Haskell/YAHT
  Learn You a Haskell -- www.learnyouahaskell.com

  Programming in Haskell -- http://www.amazon.com/Programming-Haskell-Graham-Hutton/dp/0521692695
  
There are many other books and tutorials as well -- e.g., the Gentle
Introduction to Haskell, the Haskell School of Expression, and Real
World Haskell (just google them if you are interested).  Check these
out and hopefully you can find something that you like.

-Brent

> 
> 
> Regards,
> Sayali. 
> 
> -----Original Message-----
> From: beginners-bounces at haskell.org
> [mailto:beginners-bounces at haskell.org] On Behalf Of Brent Yorgey
> Sent: Friday, December 05, 2008 6:54 PM
> To: beginners at haskell.org
> Subject: Re: [Haskell-beginners] Profiling haskell code
> 
> To get the output of one function to be the input to another, you just
> apply one to the other.  For example:
> 
>   -- This function generates a list
>   foo :: Int -> [Int]
>   foo n = [1..n]
> 
>   -- This function expects a list as input
>   bar :: [Int] -> Int
>   bar = sum . filter (>5)
> 
>   -- Use the output of foo as input to bar
>   main = print $ bar (foo 20)
> 
> Are you asking about something more than this?
> 
> -Brent
> 
> On Thu, Dec 04, 2008 at 05:42:42PM +0530, Sayali Kulkarni wrote:
> > Hey thanks Brent. This helped.
> > 
> > I have one more question now.
> > 
> > Consider I have two functions 
> > 1. gives me a range of numbers in an array.
> > 2. has to get an array input for further process.
> > 
> > Then how can I get the array generated by the first function tobe the
> > input of the second function?
> > 
> > Regards,
> > Sayali
> > 
> > -----Original Message-----
> > From: Brent Yorgey [mailto:byorgey at seas.upenn.edu] 
> > Sent: Tuesday, November 18, 2008 5:47 PM
> > To: Sayali Kulkarni
> > Subject: Re: [Haskell-beginners] Profiling haskell code
> > 
> > > I have just given it any random input array to be sorted.
> > > The commands that I had sent earlier were tried on Cygwin...
> > > (
> > > > > $ ghc --make Project.hs -prof -auto-all
> > > > >  
> > > > >  
> > > > > $ Project +RTS -p
> > > > >  ) 
> > 
> > This ought to work fine.  Just a note, to do any reasonable profiling
> > you will need to give it a *much* larger list to sort.  Otherwise it
> > will
> > execute so quickly that the timing data you get will be meaningless.
> > 
> > > 
> > > Also can you tell me any other method for profiling the code that
> you
> > > know? 
> > 
> > If you just want to see how long it takes to evaluate certain
> > expressions, you can type ':set +s' in ghci; from then on after every
> > expression you type it will tell you how long it took to evaluate and
> > how much memory was used.
> > 
> > -Brent
> > 
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
> 


More information about the Beginners mailing list