[racket] A question about code-style (and memory usage)

From: Rodolfo Carvalho (rhcarvalho at gmail.com)
Date: Sun Jul 24 00:25:31 EDT 2011

Some days ago I came across Williams'
describe<http://planet.racket-lang.org/display.ss?package=describe.plt&owner=williams>collection
from PLaneT. That's pretty awesome.
And coincidentally came this
problem<http://projecteuler.net/index.php?section=problems&id=17>from
Project Euler.

Then I wrote two solutions, and I was wondering which one of them have the
best style, according to Racket's philosophy.

#lang racket
(require (planet williams/describe/describe))

(define N 1000)

;; Solution 1
(apply + (build-list N (compose string-length integer->string add1)))

;; Solution 2
(for/fold ([sum 0])
  ([i (in-range 1 (add1 N))])
  (+ sum ((compose string-length integer->string) i)))



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.


BTW what's the recommended way to check "per-function" memory usage? There's
an unanswered question about memory
profiling<http://groups.google.com/group/racket-users/browse_thread/thread/d48ce09209a5aea4>in
the mailing list, and I could find my way out searching PLaneT, the
docs
and the mailing list archive.


[]'s

Rodolfo Carvalho
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20110724/c5847893/attachment.html>

Posted on the users mailing list.