#lang racket ; some functions from the lecture Tues. Sept. 18, 2012 (require htdp/testing) ; (drop P L) produces a list that is like L, except that ; the elements satisfying P have been dropped. (define (drop P L) (if (null? L) null (if (P (first L)) (drop P (rest L)) (cons (first L) (drop P (rest L)))))) (check-expect (drop zero? '(3 4 0 0 5 6 1 0 7 8 0)) '(3 4 5 6 1 7 8)) ; (numsFromTo M N), where (<= M N), produces ; '(M M+1 M+2 .... N). If instead (> M N), the empty list is produced. (define (numsFromTo M N) (if (> M N) null (cons M (numsFromTo (+ M 1) N)))) ; (numsTo N) specializes the result of numsFromTo to 0 as the first argument (define (numsTo N) (numsFromTo 0 N)) (check-expect (numsTo 0) '(0)) (check-expect (numsTo 1) '(0 1)) (check-expect (numsTo 2) '(0 1 2)) (check-expect (numsTo 10) '(0 1 2 3 4 5 6 7 8 9 10)) ; supply your version of mappend, which is part of assignment 2 ; Triangular array (define (triangular-array N) (map numsTo (numsTo N))) (check-expect (triangular-array 4) '((0) (0 1) (0 1 2) (0 1 2 3) (0 1 2 3 4))) ; (pairs L M) produces the list of all ordered pairs, with ; the first element from L and the second element from M (define (pairs L M) (mappend (lambda(F) (map (lambda(G) (list F G)) M)) L)) ; Method: For a given element F of L, the inner map forms the list of pairs ; with F as first element with each element G of M as the second element. ; The outer mappend is like map, creating the list of pairs with each ; element of F of L as the first element. mappend is used rather than ; map because the individual lists of pairs need to be appended. ; Otherwise we would get a list of lists of pairs, which is not desired. (check-expect (pairs '(1 2) '(3 4 5)) '((1 3) (1 4) (1 5) (2 3) (2 4) (2 5))) (generate-report)