[plt-scheme] sicp exercise 2.23

From: Veer (diggerrrrr at gmail.com)
Date: Tue Apr 29 15:35:37 EDT 2008

Hello Jason , you did not comment on my code , is it not correct approach?


>  I think you have to discard the cons form but other than the cond
>  implementation mentioned before, i'm not sure how to proceed to
>  implement it.

You can also do something like this :
;;for-each2 : (X -> Y) (listof X) -> true
;;to apply a (f X) on items of a-list , on success returns true
(define (for-each2 f a-lst)
  (and  (cons? (map f a-lst)) true))


;;always-true : X -> true
;;to produce to true for any item
(define (always-true item)
  true)

(for-each2 (lambda (x) (cond
                         [(always-true (newline)) (display x)]
                         [else (display x) ]))
           (list 57 321 88))


Thanks
Veer



On Wed, Apr 30, 2008 at 12:20 AM, Jason Wang <randomtalk at gmail.com> wrote:
> On Tue, Apr 29, 2008 at 2:03 AM, Jos Koot <jos.koot at telefonica.net> wrote:
>  >  The above form of cond has not yet been introduced either before reaching
>  > problem 2.23. Look at how you would implement map, then decide what to
>  > discard.
>  Well, in the book, they implemented map as follows:
>  (define (map proc items)
>   (if (null? items)
>       nil
>       (cons (proc (car items))
>             (map proc (cdr items)))))
>
>  I think you have to discard the cons form but other than the cond
>  implementation mentioned before, i'm not sure how to proceed to
>  implement it.
>
>
>
>  On Tue, Apr 29, 2008 at 12:35 PM, John Clements
>  <clements at brinckerhoff.org> wrote:
>  >
>  >  On Apr 28, 2008, at 11:04 PM, Jason Wang wrote:
>  > >
>  > > However, my problem is that SICP hasn't introduced begin yet...
>  > >
>  >
>  >  I don't have a copy of SICP handy (I guess I can just tear up my Schemer
>  > Membership Card right now...), but "begin" seems to me to be the natural
>  > choice to express what you want; it's true that there are other constructs
>  > that behave in a begin-like way, and in fact you can write begin as a
>  > function (for a fixed number of arguments) quite easily, but I would suggest
>  > just using 'begin', especially as you've been through HtDP already.
>
>  I wanted to understand what the intention of the author is in putting
>  this exercise here, there should be a perfectly fine implementation
>  with the knowledge in the proceeding sections, but i can't seem to
>  find it. Though i do understand that 'begin' is a rather natural way
>  to express this particular function.
>
>  Thanks a lot for all your help and any further help would be greatly
>  appreciated!
>
>
>
>  Jason
>  --
>  "It's a wonderful world hobbes ol' buddy, let's go explorin'!" -
>  Calvin in "Calvin and Hobbes"
>  _________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>


Posted on the users mailing list.