Advertisement
Guest User

2017 January

a guest
Jan 12th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 0.43 KB | None | 0 0
  1. ;;Define a Scheme function foo which finds all atoms inside an sexpression which pass a given predicate.
  2. ;;Examples:
  3. ;;(foo number? '(a (2 (c 3) 4)))
  4. ;;=> (2 3 4)
  5. ;;(foo symbol? '(a (2 (c 3) 4)))
  6. ;;=> (a c)
  7. ;;(foo symbol? 'a)
  8. ;;=> (a)
  9. ;;(foo number? 'a)
  10. ;;=> ()
  11.  
  12. (define (foo p? s)
  13.   (set! s (flatten s))
  14.     (cond
  15.       ((null? s) null)
  16.       ((p? (car s)) (cons (car s) (foo p? (cdr s))))
  17.       (else (foo p? (cdr s)))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement