<div>Some days ago I came across Williams' <a href="http://planet.racket-lang.org/display.ss?package=describe.plt&owner=williams">describe</a> collection from PLaneT. That's pretty awesome.</div><div>And coincidentally came <a href="http://projecteuler.net/index.php?section=problems&id=17">this problem</a> from Project Euler.</div>
<div><br></div><div>Then I wrote two solutions, and I was wondering which one of them have the best style, according to Racket's philosophy.</div><div><br></div><div><div>#lang racket</div><div>(require (planet williams/describe/describe))</div>
<div><br></div><div>(define N 1000)</div><div><br></div><div>;; Solution 1</div><div>(apply + (build-list N (compose string-length integer->string add1)))</div><div><br></div><div>;; Solution 2</div><div>(for/fold ([sum 0])</div>
<div> ([i (in-range 1 (add1 N))])</div><div> (+ sum ((compose string-length integer->string) i)))</div></div><div><br></div><div><br></div><div><br></div><div>For me solution 1 is compact, however it allocates the list without need (which gives out of memory error for large N). Solution 2 is more memory efficient, but more verbose.</div>
<div><br></div><div><br></div><div>BTW what's the recommended way to check "per-function" memory usage? There's an <a href="http://groups.google.com/group/racket-users/browse_thread/thread/d48ce09209a5aea4">unanswered question about memory profiling</a> in the mailing list, and I could find my way out searching PLaneT, the docs and the mailing list archive.</div>
<div><br></div><div><br></div><div>[]'s</div><br clear="all">Rodolfo Carvalho<br>