;; Sample code (and exercise solutions) from lecture notes
;; 

(define (squared x) (* x x)) 

(map squared '(1 2 3 4 5 6))


;;
;; map2 op list -> list
;;
;; (map2 op alist) creates a new list that includes one
;;   element for each pair of adjacent elements in alist.
;;   Each new element is the result of applying the operator op
;;   to the adjacent elements of alist.
;;
;;  (map2 + '(1 2 3 4)) => '(3 5 7)
;;
;; 
(define (map2 op alist)
  (cond
   [(empty? alist) empty]
   [(empty? (rest alist)) empty]
   [else
	(cons 
	 (op (first alist) (second alist))
	 (map2 op (rest alist)))]))

(map2 + '(1 2 3 4))

;;
;; map2lists op list list -> list
;;
;; (map2lists op alist blist) generates a new list in which
;;  each element is the result of applying the operator op to
;;  an element from each of alist and blist. 
;;
;;  It is expected that both lists (alist, blist) have the same
;;  number of elements.
;;
;; (map2lists + '(1 2 3 4) '(6 2 0 3)) => '(7 4 3 7)
;;

(define (map2lists op a b)
  (cond
   [(or (empty? a) (empty? b)) empty]
   [else
	(cons
	 (op (first a) (first b))
	 (map2lists op (rest a) (rest b)))]))

(map2lists + '(1 2 3 4) '(6 2 0 3))

;; ===================================
;; using build-list


(build-list 10 squared)

(define (dp x)
  (/ 1 (expt 10 (+ x 1))))

(build-list 4 dp)


;; ===================================
;;  using quicksort

(quicksort '(1 5 3 8 7 2) >)

;; computes the distance a posn is from 100,100
(define (distancefrom100100 p)
  (sqrt
   (+
	(expt (- (posn-x p) 100) 2)
	(expt (- (posn-y p) 100) 2))))

;; compare posn based on distance from 100,100
(define (cmp_posn p1 p2)
  (< (distancefrom100100 p1)
	 (distancefrom100100 p2)))

(quicksort 
 (list
  (make-posn 50 45)
  (make-posn 101 45)
  (make-posn 101 101)
  (make-posn 0 0)
  (make-posn 200 201)
  (make-posn 97 92))
 cmp_posn)