Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.haskell > #400

Re: how to compute one and filter it immediately rather than calculated all and then filter the whole

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.haskell
Subject Re: how to compute one and filter it immediately rather than calculated all and then filter the whole
Date 2016-07-21 16:31 +0100
Organization A noiseless patient Spider
Message-ID <87lh0vatz3.fsf@bsb.me.uk> (permalink)
References <3f8a8fce-1bce-4ae3-b8aa-8d0a663c2c94@googlegroups.com>

Show all headers | View raw


meInvent bbird <jobmattcon@gmail.com> writes:

> *Main> let true3print = [(peval (replacewithoriginalformula
> (replacewithoriginalformula (fst (bb3true!!i)) bb2map) bb1map),
> isInfixOf "M8" (peval (replacewithoriginalformula
> (replacewithoriginalformula (fst (bb3true!!i)) bb2map) bb1map))) | i
> <- [0..(99999-1)]]
> *Main> let true3M8 = [ x | (x,True) <- true3print ] 
>
> above code, it calculated all in true3print first
>
> and then filter the whole result

Have you head of lazy evaluation?  The list true3print is probably not
calculated first, but only as needed.  It will probably never exist in
memory all at the same time.

> is it possible to calculated each and filter immediately and saved
> into list?

Yes, of course.  Your example is a bit messy so I don't really want to
try re-write it but you can generate only the (_, True) values in a
single list comprehension.

> if compute in this way, can it be faster?

That depends on the calculations.  It won't be significantly faster
unless you can skip some big calculation for the False values and
Haskell is clever.  Take this:

  list = [(g x, f x) | x <- [1..20]]

Neither f not g get get called yet, and if we filter like this:

  [x | (x,True) <- list]

the function g is only called for those values which match (_,True).  In
other words, what might look faster:

  [g x | x <- [1..20], f x]

does exactly the same calculation.

> how to compute one and filter it immediately rather than calculated
> all and then filter the whole

You can do it just by adding a clause in the list comprehension as I did
in the last example.

-- 
Ben.

Back to comp.lang.haskell | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

how to compute one and filter it immediately rather than calculated all and then filter the whole meInvent bbird <jobmattcon@gmail.com> - 2016-07-21 05:07 -0700
  Re: how to compute one and filter it immediately rather than calculated all and then filter the whole Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-07-21 16:31 +0100
    Re: how to compute one and filter it immediately rather than calculated all and then filter the whole meInvent bbird <jobmattcon@gmail.com> - 2016-07-21 19:29 -0700
      Re: how to compute one and filter it immediately rather than calculated all and then filter the whole meInvent bbird <jobmattcon@gmail.com> - 2016-07-21 19:47 -0700
        Re: how to compute one and filter it immediately rather than calculated all and then filter the whole meInvent bbird <jobmattcon@gmail.com> - 2016-07-21 21:38 -0700
          Re: how to compute one and filter it immediately rather than calculated all and then filter the whole Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-07-22 09:40 +0100
            Re: how to compute one and filter it immediately rather than calculated all and then filter the whole meInvent bbird <jobmattcon@gmail.com> - 2016-07-22 01:57 -0700
              Re: how to compute one and filter it immediately rather than calculated all and then filter the whole meInvent bbird <jobmattcon@gmail.com> - 2016-07-22 02:02 -0700
                Re: how to compute one and filter it immediately rather than calculated all and then filter the whole meInvent bbird <jobmattcon@gmail.com> - 2016-07-22 02:04 -0700
                Re: how to compute one and filter it immediately rather than calculated all and then filter the whole meInvent bbird <jobmattcon@gmail.com> - 2016-07-22 04:33 -0700
                Re: how to compute one and filter it immediately rather than calculated all and then filter the whole meInvent bbird <jobmattcon@gmail.com> - 2016-07-23 02:36 -0700
            Re: how to compute one and filter it immediately rather than calculated all and then filter the whole meInvent bbird <jobmattcon@gmail.com> - 2016-07-25 00:55 -0700

csiph-web