fork download
  1. ; folds
  2.  
  3. (define (foldr f a xs)
  4. (if (null? xs) a
  5. (f (car xs) (foldr f a (cdr xs)))))
  6.  
  7. (define (foldl f a xs)
  8. (if (null? xs) a
  9. (foldl f (f a (car xs)) (cdr xs))))
  10.  
  11. (define (foldr1 f xs)
  12. (if (null? (cdr xs)) (car xs)
  13. (f (car xs) (foldr1 f (cdr xs)))))
  14.  
  15. (define (foldl1 f xs)
  16. (foldl f (car xs) (cdr xs)))
  17.  
  18. (define (scan f a xs)
  19. (define (scanx f a xs)
  20. (if (null? xs) xs
  21. (scan f (f a (car xs)) (cdr xs))))
  22. (cons a (scanx f a xs)))
  23.  
  24. (display (foldr + 0 '(1 2 3 4))) (newline)
  25. (display (foldl + 0 '(1 2 3 4))) (newline)
  26. (display (foldr cons '() '(1 2 3 4))) (newline)
  27. (display (foldl cons '() '(1 2 3 4))) (newline)
  28. (define (plusone _ n) (+ n 1))
  29. (display (foldr plusone 0 '(1 2 3 4))) (newline)
  30. (define (snoc xs x) (cons x xs))
  31. (display (foldl snoc '() '(1 2 3 4))) (newline)
  32. (display (foldr1 max '(1 2 3 4))) (newline)
  33. (display (foldl1 max '(1 2 3 4))) (newline)
  34. (display (scan + 0 '(1 2 3 4))) (newline)
  35. (display (map reverse (scan snoc '() '(1 2 3 4)))) (newline)
Success #stdin #stdout 0.04s 8200KB
stdin
Standard input is empty
stdout
10
10
(1 2 3 4)
((((() . 1) . 2) . 3) . 4)
4
(4 3 2 1)
4
4
(0 1 3 6 10)
(() (1) (1 2) (1 2 3) (1 2 3 4))