Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Fold

Name: Anonymous 2008-02-02 15:04

http://www.haskell.org/haskellwiki/Fold
Haskell has map in terms of 'foldr ((:) . f) []'

How would you implementd foldr, and then map with foldr in scheme/lisp?

Name: Anonymous 2008-02-02 15:29


(define (foldr f z xs)
(if (null? xs)
z
(f (car xs) (foldr f z (cdr xs)))))

Name: Anonymous 2008-02-02 15:33

>>2
read my question, stop posting code from wikipedia.
Plus, your code is not tail-recursive.

Name: Anonymous 2008-02-02 15:36

(define (map f seq)
  (foldr (lambda (x y) (cons (f x) y)) '() seq))

Name: Anonymous 2008-02-02 15:56

>>4
Ah, thanks.
Is it possible to write a compose function in scheme?
Something like

(define (compose f g)
  (f (lambda (x) (g x))))

But that doesn't work

Name: Anonymous 2008-02-02 16:01

(define (compose f g)
  (lambda (x) (f (g x))))

Name: Anonymous 2008-02-02 16:01

>>5


(define (compose f g)
  (lambda (x) (f (g x))))

Name: Anonymous 2008-02-02 16:10

>>5-7

(define (compose . l)
  (lambda (x)
    (define (compose-iter l x)
       (if (null? l) x
           (compose-iter (cdr l) ((car l) x))))
     (compose-iter l x)))

Name: Anonymous 2008-02-02 16:26

>>1
Read SICP.

Name: Anonymous 2008-02-02 16:27

>>8
> ((compose sqr sub1) 5)    
24

(define (compose . l)
  (lambda (x)
    (foldr (lambda (f x) (f x)) x l)))

((compose sqr sub1) 5)
16


Name: Anonymous 2008-02-02 16:37

>>10
oh, fuck

fixed

(define (compose . l)
  (lambda (x)
    (define (compose-iter l x)
       (if (null? l) x
           (compose-iter (cdr l) ((car l) x))))
     (compose-iter (reverse l) x)))

Name: Anonymous 2008-02-02 20:14

>>11
foldr is superior. you have a fundamental list iterator, you may as well use it.

Name: Anonymous 2008-02-02 20:15

(define ((compose . l) x)
  (foldr (lambda (f x) (f x)) x l)))

Name: 11 2008-02-02 20:38

>>12
scheme does not provide foldr, and your definition of foldr is not tail recursive, which makes my solution ``better''.
Unless you define a tail-recursive foldr I see no reason why one would prefer your solution.

Name: Anonymous 2008-02-03 7:41

>>14
Why do you want a tail recursive foldr? If you want a iterative process use foldl.

Name: Anonymous 2008-02-03 7:51

>>15
I'm merely stating that I'd choose my solution over his because mine is tail recursive. (As long as reverse is tail recursive too)

Name: Anonymous 2009-03-18 3:52

I'm feeling really keen, for some of that good ol' green

Marijuana MUST be legalized.

Name: ​​​​​​​​​​ 2010-10-23 11:44

Don't change these.
Name: Email:
Entire Thread Thread List