Hi Rodolfo,

I think solution 2 is generally preferred, and
solution 1 would be considered a slight abuse of apply.

On the other hand, I find that summing comes up sufficiently
often to deserve its own compact syntax, so I propose:

;; Solution 3
(require (planet "mathsymbols.rkt" ("stephanh" "mathsymbols.plt" 1 0)))

(Σ ([i (in-range 1 (add1 N))])
       (string-length (integer->string i)))

Stephan


On 07/24/2011 06:25 AM, Rodolfo Carvalho wrote:
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



_________________________________________________
   For list-related administrative tasks:
   http://lists.racket-lang.org/listinfo/users
_________________________________________________
 For list-related administrative tasks:
 http://lists.racket-lang.org/listinfo/users

Reply via email to