Just to be sure, because I am not quite familiar with the dark hairy internals of GHC:<br><br>> Of course, given a type signature that allows strictness to be inferred.<br><br>You mean a signature with no type variables and types that are know to GHC as being strict?<br>
(Like <span style="font-family: courier new,monospace;">Int -> Int -> Int</span> instead of <span style="font-family: courier new,monospace;">(Num a) => a -> a -> a</span>)<br><br>> The difference is that the explicit recursion produces the better code even<br>
> with optimisations turned off, except that the overload of (+) to use is<br>
> not inlined, so the accumulator still builds a thunk, while with<br>
> optimisations you get the specialised strict additions (+# resp.<br>
> plusInteger, ...) so you have the strictness you need.<br><br>(+#) is then the GHC's strict equivalent of (+)?<br>But if you make an overlay to (+), like, say:<br><br>(?) :: (Num a) => a -> a -> a<br>a ? b = a + b<br>
<br>Then (?) will be lazy, won't it?<br>Then optimizations will not occur, <span style="font-family: courier new,monospace;">a ? b</span> will remain a thunk and not be replaced by <span style="font-family: courier new,monospace;">a +# b</span> and be strictly evaluated?<br>
<br>If so, then it means that you can always turn a strict function into a non strict one, am I right?<br><br><br><div class="gmail_quote">2011/3/31 Daniel Fischer <span dir="ltr"><<a href="mailto:daniel.is.fischer@googlemail.com">daniel.is.fischer@googlemail.com</a>></span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div class="im">On Thursday 31 March 2011 11:45:00, Christian Maeder wrote:<br>
> Since we don't have a function sum' in the Prelude (should we have it?)<br>
<br>
</div>I think we should.<br>
<div class="im"><br>
> I wonder what happens if you just use "sum". Will the "sum" (based on<br>
> sum' so without -DUSE_REPORT_PRELUDE) be strict enough?<br>
<br>
</div>I don't know about other compiler's behaviour, but for GHC, it will be<br>
strict enough *if compiled with optimisations*, but not without (the<br>
strictness analyser runs only with optimisations turned on).<br>
- Of course, given a type signature that allows strictness to be inferred.<br>
<br>
However, the same holds for 'foldl (+) 0'. In fact, in the presence of a<br>
suitable type signature, with optimisations turned on, both produce nearly<br>
identical code (the order of parameters in the recursive loop is changed,<br>
sometimes parameter order can make a surprisingly large difference, but<br>
whether it's better to have the list or the accumulator first depends).<br>
<br>
The difference is that the explicit recursion produces the better code even<br>
with optimisations turned off, except that the overload of (+) to use is<br>
not inlined, so the accumulator still builds a thunk, while with<br>
optimisations you get the specialised strict additions (+# resp.<br>
plusInteger, ...) so you have the strictness you need.<br>
<div class="im"><br>
><br>
> #ifdef USE_REPORT_PRELUDE<br>
> sum = foldl (+) 0<br>
> product = foldl (*) 1<br>
> #else<br>
> sum l = sum' l 0<br>
> where<br>
> sum' [] a = a<br>
> sum' (x:xs) a = sum' xs (a+x)<br>
> product l = prod l 1<br>
> where<br>
> prod [] a = a<br>
> prod (x:xs) a = prod xs (a*x)<br>
> #endif<br>
><br>
> Cheers C.<br>
><br>
> P.S.<br>
><br>
> isMultiple a = any ((== 0) . mod a)<br>
<br>
</div>For Int (and most other types),<br>
<br>
isMultiples a = any ((== 0) . rem a)<br>
<br>
will be faster (mod is implemented using rem).<br>
However, for checks other than comparisons with 0, one needs to be aware of<br>
the differences of rem and mod, the latter does what one would expect, rem<br>
can badly surprise the unaware.<br>
<div><div></div><div class="h5"><br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
</div></div></blockquote></div><br>