(hello

‘world)

One more time!

Now we’re getting ugly. Hmmmmmm.

(define (onemoretime fails? lst func)
  
  ;; acc is usually the result of the
  ;; "reduce so far", here it is the
  ;; last pair in newlst. cur is
  ;; current item of consideration
  ;; in the original list. 
  (define (foo acc cur)
    ;; func should return a new
    ;; pair, which gets tacked
    ;; onto the end of newlst.
    (set-cdr! acc (func cur))
    ;; Have lfold push the new
    ;; pair into acc next
    ;; time it loops.
    (rest acc))

  (define (bar acc cur)
    ;; Here, func is allowed to 
    ;; return #f signalling to 
    ;; leave acc alone.
    (let ((tst (func cur)))
      (if tst
	  (begin (set-cdr! acc tst)
		 (rest acc))
	  acc)))

  ;; Grab pointer to first cons.
  ;; Contents of car will be dropped.
  ;; Contents of cdr will be clobbered. 
  (let ((newlst (list #f))) 
    (lfold (if fails? bar foo)
	   newlst ; Accumulator
	   lst) ; list we're deriving from
    (rest newlst))) ; car is trash, see above.

Examples so far:

(define (append hd tl)
  (onemoretime #f hd (lambda (nxt) 
                                   (cons nxt tl))))

(define (map func lst)
  (onemoretime #f lst (lambda (nxt) 
                                   (list (func nxt)))))

(define (filter pred? lst)
  (onemoretime #t lst (lambda (nxt)
			           (if (pred? nxt)
			               (list nxt)
			               #f))))

August 5, 2010 Posted by | Programming, Scheme | Leave a Comment

Update to my toy.

Function may return #f rather than cons, which will leave the list it’s building as is. Was needed for filter. Also slows it down a great deal. :[

(define (stillhasnoname lst func)
  ;; Grab pointer to first cons.
  ;; Contents of car will be dropped.
  ;; Contents of cdr will be clobbered. 
  (let ((newlst (list #f))) 
    ;; acc is usually the result of the
    ;; "reduce so far", here it is the
    ;; last pair in newlst. cur is
    ;; current item of consideration
    ;; in the original list. 
    (lfold (lambda (acc cur)
	     ;; func should return either 
	     ;; a new pair to be tacked 
	     ;; onto the end of newlst,
	     ;; or #f which will leave
	     ;; acc as-is. 
	     (let ((tst (func cur)))
	       (if tst
		   (begin (set-cdr! acc tst)
			  (rest acc))
		   acc)))
           newlst ; Accumulator
           lst) ; list we're deriving from
    (rest newlst))) ; car is trash, see above.

August 5, 2010 Posted by | Programming, Scheme | Leave a Comment

In case I want to grok this in 6 months.

(define (whatdoicallthis lst func)
  ;; Grab pointer to first cons.
  ;; Contents of car will be dropped.
  ;; Contents of cdr will be clobbered. 
  (let ((newlst (list #t))) 
    ;; acc is usually the result of the
    ;; "reduce so far", here it is the
    ;; last pair in newlis. cur is
    ;; current item of consideration
    ;; in the original list. 
    (lfold (lambda (acc cur)
	     ;; func should return a new
	     ;; pair, which gets tacked 
	     ;; onto the end of newlst.
	     (set-cdr! acc (func cur))
	     ;; Have lfold push the new
	     ;; pair into acc next
	     ;; time it loops.
	     (rest acc))
	   newlst ; Accumulator
	   lst) ; list we're deriving from
    (rest newlst))) ; car is trash, see above.

(define (append hd tl)
  (whatdoicallthis hd (lambda (nxt) 
			(cons nxt tl))))

(define (map func lst)
  (whatdoicallthis lst (lambda (nxt) 
			 (list (func nxt)))))

August 1, 2010 Posted by | Programming, Scheme | Leave a Comment

Woo!

Got it.

(define (whatdoicallthis lst func)
(let ((newlst (list list)))
(lfold (lambda (hd tl)
(set-cdr! hd (func tl))
(rest hd))
newlst
lst)
(rest newlst)))

(define (append hd tl)
(whatdoicallthis hd (lambda (nxt) (cons nxt tl))))

(define (map func lst)
(whatdoicallthis lst (lambda (nxt) (list (func nxt)))))

August 1, 2010 Posted by | Programming, Scheme, Uncategorized | Leave a Comment

A step closer…

(define (append hd tl)
(let ((newlst (list list)))
(lfold (lambda (hd nxt)
(set-cdr! hd (cons nxt tl))
(rest hd))
newlst
hd)
(rest newlst)))

August 1, 2010 Posted by | Programming, Scheme | Leave a Comment

   

Follow

Get every new post delivered to your Inbox.