fork download
  1. ;#!/usr/bin/racket
  2. ;#lang scheme
  3.  
  4. (define (reverse-r sequence)
  5. (foldr (lambda (x y) (append y (list x))) '() sequence))
  6.  
  7. (define (reverse-l sequence)
  8. (foldl (lambda (x y) (append (list x) y)) '() sequence))
  9.  
  10. ; Test
  11. (define seq (list 1 2 3 4 5 6 7))
  12.  
  13. (display (reverse-r seq))
  14. (newline)
  15.  
  16. (display (reverse-l seq))
  17. (newline)
  18.  
Success #stdin #stdout 0s 7276KB
stdin
Standard input is empty
stdout
(7 6 5 4 3 2 1)
(((((((() . 1) . 2) . 3) . 4) . 5) . 6) . 7)