; CSE 341, 99su
; Assignment 1:  Scheme Warmup
; sample solutions
; yasuhara@cs, 1999-07-01


; no. 1
; ----------------------------------------------------------------------

(define (abs-val x)
  "returns the absolute value of the number x"
  (if (< x 0) -x x))

; no. 2
; ----------------------------------------------------------------------

(define (square-list l)
  "returns a list of the squares of the elements in the list l"
  (if (null? l)
      '()
      (cons (* (car l) (car l)) (square-list (cdr l)))))

; no. 2, bonus
; ----------------------------------------------------------------------

(define (square-list-alt l)
  "alternate version of square-list using map"
  (map (lambda (x) (* x x)) l))

(define (square-list-alt2 l)
  (define (square x) (* x x))
  "another alt. version of square-list with map and a nested function def."
  (map square l))

; no. 3
; ----------------------------------------------------------------------

(define (filter-ge l k)
  "returns a list of the elements of number list l that are >= k"
  (if (null? l)
      '()
      (if (>= (car l) k)
          (cons (car l) (filter-ge (cdr l) k))
          (filter-ge (cdr l) k))))

; no. 4
; ----------------------------------------------------------------------

(define (filter p? l)
  "returns a list of the elements of list l for which unary predicate p? is true" 
  (if (null? l)
      '()
      (if (p? (car l))
          (cons (car l) (filter p? (cdr l)))
          (filter p? (cdr l)))))

; no. 3 bonus (full credit)
; ----------------------------------------------------------------------

(define (filter-ge-alt l k)
  "alt. version of filter-ge using filter"
  (filter (lambda (x) (>= x k)) l))

(define (filter-ge-alt2 l k)
  (define (ge-k x) (>= x k))
  "another alt. version of filter-ge using a nested function def."
  (filter ge-k l))


; end of file