[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Greetings




Hi,

I wanted to say thanks to everyone involved with this project.
Recently I've decided to learn Scheme as an alternative to C++ which
is my primary professional language.  I saw a study done comparing
lisp/scheme against C++ and Java and was surprised at how well it had
done (it beat them both quite handily, using mzscheme.)  I first tried
out MIT scheme but couldn't figure out how to really "do" anything in
its REPL.  Surely there must be a way that people use it but it just
seems very difficult when I just wanted to learn the language.
DrScheme was by far the best environment I've found to use.  Many kudos!

Anyway, I found DrScheme due to its relationship with mzscheme (which
I found in the report) and independently started studying this
language.  With pretty much just the online help, followed by a
careful study of "The Little Schemer" I've worked myself through a lot
of the material and am starting (I think) to understand this language.
It strikes me as elegant and simplistic, yet very powerful.  (Surely
I'm preaching to the choir on that!)  

I wish I could have an opportunity to study this in more depth in a
formal environment, but for now I'm stuck by myself without anyone to
whom I may any questions.  So I'm hoping that someone would be willing to
answer a question that I'm stuck on.  (If this is the wrong newsgroup,
please accept my apologies and let me know where I should ask this.
It is the only general discussion group I found listed in the online
help.) 

In an attempt to solve a "simple" problem, I wrote a recursive
algorithm to visit each n-sized unique combination of elements in a
list (which contains at least n elements.)  I have it working to my
satisfaction, but I'm afraid I'm missing something, because Mr. Spidy
is not liking some of my code, and I am having trouble figuring out
why.  (And I'm not the kind of person who likes to overlook any
warnings.  My C++ code is compiled in -Wall and -pedantic mode on
g++.)

Here is the code:


(define (visit lst)
  (printf "Current combination: ~a~n" lst))


;;
;; generate all n combinations of items in list "lst"
;;
;; The premise here is for each combination of size n of items in the
;;set called "avail"
;; I can take one element from
;; the "available" set and bind it to an "in-use" set.  Then I can
;;simplify the problem
;; to (n-1) combinations of the remaining "available" set, with the
;;"in-use" set appended 
;; to the beginning.
;;
(define (choose m lst)
  (define choose-internal 
    (lambda (n  in-use  avail  num-avail)
      (cond ([null? avail] null)
            ([= 1 n]
             (visit (cons (car avail) in-use))
             (choose-internal n in-use (cdr avail) (sub1 num-avail)))
            (else (let loop ([cur avail]
                             [len num-avail])
                    (if (>= len n)
                        (begin
                          (choose-internal (sub1 n) 
                                           (cons (car cur) in-use) ;<--HERE
                                           (cdr cur)
                                           (sub1 len))
                          (loop (cdr cur) (sub1 len)))))))))
  (if (> m 0)
      (choose-internal m null lst (length lst)) 
      #f))

(choose 3 '(a b c d e))



(The problem is marked "HERE".  The 'car' call is in red, but my
program appears to work correctly, and I don't see how that call could
fail.

If anyone could explain what I've done wrong to warrent this warning,
I'd be very thankful.

(If you have any other suggestions about this code, I'd also love to
hear them!)  

Thanks!

-- 
Chris