Send Beginners mailing list submissions to
        beginners@haskell.org

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
        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.  Caching evaluation of lazy lists (Philip Scott)
   2. Re:  Caching evaluation of lazy lists (Daniel Fischer)
   3. RE:  how to set command args in ghci (Kui Ma)
   4. Re:  Haskell Output Help (Chadda? Fouch?)
   5. Re:  how to set command args in ghci (Chadda? Fouch?)
   6. Re:  \x -> x < 0.5 && x > -0.5 (pl)
   7. Re:  Haskell Output Help (Brent Yorgey)
   8. Re:  \x -> x < 0.5 && x > -0.5 (Daniel Fischer)
   9. Re:  \x -> x < 0.5 && x > -0.5 (Darrin Thompson)


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

Message: 1
Date: Fri, 23 Oct 2009 12:51:52 +0100
From: Philip Scott <haskell-beginn...@foo.me.uk>
Subject: [Haskell-beginners] Caching evaluation of lazy lists
To: beginners@haskell.org
Message-ID: <4ae198d8.8090...@foo.me.uk>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Hi folks,

Quick question for you. Suppose I have some lazy list, x, which is very 
expensive to evaluate, and I want to do something with it more than 
once; for example:

f x = g (take 5 x) (take 6 x)

Is Haskell clever enough to only evaluate the list as much as is needed, 
or will it evaluate it once to get the first five values, and again to 
get the first 6 (when really it only needed to get one more). Or is it 
really really clever, and works out it needs to take 6 to begin with, 
then just give you the first 5 for the first call?

More tricky - is there a way to make that cache (if it exsists) persist 
in an interactive session? Eventually I am intending to write my own 
application with a little console at the bottom which does some 
ghci-esque magic, but for now lets say I am in ghci and I want to call f 
again with the same list. Is there a way to avoid it from forcing a 
recomputation of my lazy list?

Any advice greatly appreciated,

Philip


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

Message: 2
Date: Fri, 23 Oct 2009 15:08:03 +0200
From: Daniel Fischer <daniel.is.fisc...@web.de>
Subject: Re: [Haskell-beginners] Caching evaluation of lazy lists
To: beginners@haskell.org
Message-ID: <200910231508.03578.daniel.is.fisc...@web.de>
Content-Type: text/plain;  charset="iso-8859-1"

Am Freitag 23 Oktober 2009 13:51:52 schrieb Philip Scott:
> Hi folks,
>
> Quick question for you. Suppose I have some lazy list, x, which is very
> expensive to evaluate, and I want to do something with it more than
> once; for example:
>
> f x = g (take 5 x) (take 6 x)
>
> Is Haskell clever enough to only evaluate the list as much as is needed,

In that example, when you call "f (makeExpensiveList arg1 arg2)", the list is 
bound to the 
name x in the body of f, so g's arguments are taken from the very same list, 
which is 
evaluated only once. Each of the list elements is evaluated when it's needed, 
so maybe 
there will be fewer than 6 elements evaluated.
However, if you call "f (makeExpensiveList arg1 arg2)" again later in the 
programme with 
the same arg1 and arg2, it will be probably evaluated again (the optimiser 
would need to 
see that it's called with the same arguments again and that it's worth caching 
the result 
to avoid that).

If your expensive list is not generated by a function but a constant (like the 
list of 
Fibonacci numbers), bind it to a name

expensiveList = ...

and it will be cached between calls to f (unless memory pressure forces it to 
be garbage 
collected between calls and then re-evaluated).
If it's generated by a function, give names to the lists obtained from 
frequently used 
arguments:

exList_1_2 = makeExpensiveList 1 2
exList_4_0 = makeExpensiveList 4 0
...

cachedExpensiveList 1 2 = exList_1_2
cachedExpensiveList 4 0 = exList_4_0
...
cachedExpensiveList a b = makeExpensiveList a b


> or will it evaluate it once to get the first five values, and again to
> get the first 6 (when really it only needed to get one more). Or is it
> really really clever, and works out it needs to take 6 to begin with,
> then just give you the first 5 for the first call?

The order of evaluation is determined by data-dependencies, so it may evaluate 
the list in 
order, or evaluate the sixth element first, then the first, after that the 
third,...

>
> More tricky - is there a way to make that cache (if it exsists) persist
> in an interactive session? Eventually I am intending to write my own
> application with a little console at the bottom which does some
> ghci-esque magic, but for now lets say I am in ghci and I want to call f
> again with the same list. Is there a way to avoid it from forcing a
> recomputation of my lazy list?

Give it a name:

let el = makeExpensiveList args

then, barring memory pressure forcing it out, it will be computed only once 
(each list 
element will be computed only once, when it's first needed).

>
> Any advice greatly appreciated,
>
> Philip




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

Message: 3
Date: Fri, 23 Oct 2009 21:26:03 +0800
From: Kui Ma <mkl...@hotmail.com>
Subject: RE: [Haskell-beginners] how to set command args in ghci
To: <nonow...@gmail.com>
Cc: beginners@haskell.org
Message-ID: <blu138-w3201fc3849fc07a22bf71ac9...@phx.gbl>
Content-Type: text/plain; charset="gb2312"


Thank  you very much!
 
> Date: Fri, 23 Oct 2009 19:40:46 +0900
> Subject: Re: [Haskell-beginners] how to set command args in ghci
> From: nonow...@gmail.com
> To: mkl...@hotmail.com
> CC: beginners@haskell.org
> 
> :set args foo bar
> 
> should work. FYI, type :? to ghci to see all commands.
> 
> HTH
> 
> -nwn
> 
> 2009/10/23 Kui Ma <mkl...@hotmail.com>:
> > Is it possible to set the Command Arguments when running the main function
> > in ghci?
> >
> > Thanks,
> > Kui
> >
> >
> >
> > ________________________________
> > Windows Live: Friends get your Flickr, Yelp, and Digg updates when they
> > e-mail you.
> > _______________________________________________
> > Beginners mailing list
> > Beginners@haskell.org
> > http://www.haskell.org/mailman/listinfo/beginners
> >
> >
                                          
_________________________________________________________________
Windows Live: Friends get your Flickr, Yelp, and Digg updates when they e-mail 
you.
http://www.microsoft.com/middleeast/windows/windowslive/see-it-in-action/social-network-basics.aspx?ocid=PID23461::T:WLMTAGL:ON:WL:en-xm:SI_SB_3:092010
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20091023/b0d24f2f/attachment-0001.html

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

Message: 4
Date: Fri, 23 Oct 2009 15:39:26 +0200
From: Chadda? Fouch? <chaddai.fou...@gmail.com>
Subject: Re: [Haskell-beginners] Haskell Output Help
To: chandni...@yahoo.com
Cc: beginners@haskell.org
Message-ID:
        <e9350eaf0910230639m4dc41a9cmad994b985b489...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

On Thu, Oct 22, 2009 at 9:11 PM, Chandni Navani <chandni...@yahoo.com>wrote:

> I have a list of lists which all contain strings.  [[String]].  I need to
> figure out how to print them so that after each individual string, there is
> a new line.
>
> If this is the initial list [["abc", "cde"] ["fgh", "ghi"]]
> [["abc"
>   "cde"]
>  ["fgh",
>   "ghi"]]
>
> Can anyone help me figure this out? Thanks.
>
>
You can use unlines and concat :

> putStr . unlines . concat $ [["abc", "cde"] ["fgh", "ghi"]]

-- 
Jedaï
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20091023/04ca00dc/attachment-0001.html

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

Message: 5
Date: Fri, 23 Oct 2009 15:41:13 +0200
From: Chadda? Fouch? <chaddai.fou...@gmail.com>
Subject: Re: [Haskell-beginners] how to set command args in ghci
To: Yusaku Hashimoto <nonow...@gmail.com>
Cc: beginners@haskell.org
Message-ID:
        <e9350eaf0910230641m31ffcbd1oa1cc6bc5cfab4...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

On Fri, Oct 23, 2009 at 12:43 PM, Yusaku Hashimoto <nonow...@gmail.com> wrote:
> or
>
> :ma foo bar
>

I think this example would be more self-explanatory if you used the
whole command name :

ghci> :main args1 args2

-- 
Jedaï


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

Message: 6
Date: Fri, 23 Oct 2009 16:25:57 +0200
From: pl <pl.lis...@gmail.com>
Subject: Re: [Haskell-beginners] \x -> x < 0.5 && x > -0.5
To: Michael Mossey <m...@alumni.caltech.edu>
Cc: beginners@haskell.org
Message-ID:
        <7b6764f30910230725v7e3b03cdj2e75efddd5fbd...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

   filter ((<=0.5) . abs) xs


On Mon, Oct 19, 2009 at 10:49 AM, Michael Mossey <m...@alumni.caltech.edu>wrote:

> Is there a nifty way to write
>
> filter (\x -> x < 0.5 && x > -0.5) xs
>
> without explicitly using x?
>
> Maybe arrows? I have a vague understanding that arrows can "send" an
> argument to more than one computation.
>
> -Mike
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20091023/28567100/attachment-0001.html

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

Message: 7
Date: Fri, 23 Oct 2009 10:26:04 -0400
From: Brent Yorgey <byor...@seas.upenn.edu>
Subject: Re: [Haskell-beginners] Haskell Output Help
To: beginners@haskell.org
Message-ID: <20091023142604.ga24...@seas.upenn.edu>
Content-Type: text/plain; charset=iso-8859-1

On Thu, Oct 22, 2009 at 12:11:07PM -0700, Chandni Navani wrote:
> I have a list of lists which all contain strings.  [[String]].  I need to 
> figure out how to print them so that after each individual string, there is a 
> new line.
> 
> If this is the initial list [["abc", "cde"] ["fgh", "ghi"]]
> [["abc"
>   "cde"]
>  ["fgh",
>   "ghi"]]
> 
> Can anyone help me figure this out? Thanks.

Most of the other solutions I've seen people post would output something like

abc
cde
fgh
ghi

But if you actually want brackets and quotes etc. to show the
structure, you could do something like

putStrLn . bracket . intercalate ",\n " .  map (bracket . intercalate ",\n  " . 
map show)
  where bracket x = "[" ++ x ++ "]"

Just transform the list step-by-step into the particular String you want as 
output.

-Brent


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

Message: 8
Date: Fri, 23 Oct 2009 17:23:23 +0200
From: Daniel Fischer <daniel.is.fisc...@web.de>
Subject: Re: [Haskell-beginners] \x -> x < 0.5 && x > -0.5
To: beginners@haskell.org
Message-ID: <200910231723.23978.daniel.is.fisc...@web.de>
Content-Type: text/plain;  charset="iso-8859-15"

Am Freitag 23 Oktober 2009 16:25:57 schrieb pl:
>    filter ((<=0.5) . abs) xs

Would be filter ((< 0.5) . abs). Yes, it's fine.

However, if the range is not symmetrical about 0, this is not so nice anymore:

cap lo hi = filter (\x -> lo < x && x < hi)

cap lo hi = filter (liftM2 (&&) (lo <) (< hi))

cap lo hi = filter ((&&) <$> (lo <) <*> (< hi))

cap lo hi = filter ((< halflength) . abs . subtract mid)
      where
        mid = (lo+hi)/2
        halflength = (hi-lo)/2

>
> On Mon, Oct 19, 2009 at 10:49 AM, Michael Mossey 
> <m...@alumni.caltech.edu>wrote:
> > Is there a nifty way to write
> >
> > filter (\x -> x < 0.5 && x > -0.5) xs
> >
> > without explicitly using x?
> >
> > Maybe arrows? I have a vague understanding that arrows can "send" an
> > argument to more than one computation.
> >
> > -Mike



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

Message: 9
Date: Fri, 23 Oct 2009 11:25:57 -0400
From: Darrin Thompson <darri...@gmail.com>
Subject: Re: [Haskell-beginners] \x -> x < 0.5 && x > -0.5
To: beginners@haskell.org
Message-ID:
        <a2e649c70910230825t3fa93c0clb735bc4193a6...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

On Fri, Oct 23, 2009 at 10:25 AM, pl <pl.lis...@gmail.com> wrote:
>
>    filter ((<=0.5) . abs) xs
>

pure (&&) <*> (< 0.5) <*> (> -0.5)

liftM2 (&&) (< 0.5) (> -0.5)

Someone suggested that this was an example of the reader monad but I
don't get that.

> :i (->)
data (->) a b   -- Defined in GHC.Prim
instance Monad ((->) r) -- Defined in Control.Monad.Instances
instance Functor ((->) r) -- Defined in Control.Monad.Instances
instance Applicative ((->) a) -- Defined in Control.Applicative

That's what I see working here.

--
Darrin


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

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 16, Issue 18
*****************************************

Reply via email to