[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'
from PLaneT. That's pretty awesome.
And coincidentally came this
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
the mailing list, and I could find my way out searching PLaneT, the
and the mailing list archive.


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.