Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2021
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 1.38 KB | None | 0 0
  1. (display
  2.  (letrec* ([the-list (read)]
  3.            [even-elts (lambda (l)
  4.                         (if (or (null? l)
  5.                                 (null? (cdr l)))
  6.                             '()
  7.                             (cons (cadr l)
  8.                                   (even-elts (cddr l)))))]
  9.            [the-even-elts (even-elts the-list)]
  10.            [sorted? (lambda (l)
  11.                       (if (null? l)
  12.                           #t
  13.                           (let ([tail (cdr l)])
  14.                             (if (null? tail)
  15.                                 #t
  16.                                 (and (< (car l) (car tail))
  17.                                      (sorted? tail))))))]
  18.            [positive-indices-iter (lambda (l index indices)
  19.                                     (if (null? l)
  20.                                         indices
  21.                                         (positive-indices-iter
  22.                                          (cdr l)
  23.                                          (1+ index)
  24.                                          (if (negative? (car l))
  25.                                              indices
  26.                                              (append indices (list index))))))]
  27.            [positive-indices (lambda (l) (positive-indices-iter l 0 '()))])
  28.    (if (sorted? the-even-elts)
  29.        (fold-left * 1 the-even-elts)
  30.        (positive-indices the-list))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement