#lang racket ;;;;********************************************************************** ;;; CS251 Spring 2018 PS6 ;;; Your name: ;;;;********************************************************************** ;;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ;;; Solo Problem 1: Diagonal Duples ;;;----------------------------------------------------------------------- ;; You are given the following function. ;; In Problem 1a, explain what it returns (define (diagonal-duples n) (foldr append null (map (λ (sum) (map (λ (fst) (list fst (- sum fst))) (range 0 (+ sum 1)))) (range 0 (+ n 1))))) ;;;----------------------------------------------------------------------- ;; Flesh out the following skeleton for Problem 1b (define (diagonal-duples-genlist-apply n) ; Assume is n a nonnegative integer (genlist-apply 'next-function-goes-here 'done-function-goes-here 'keep-done-value-goes-here 'seed-goes-here )) ;;;----------------------------------------------------------------------- ;; Flesh out the following skeleton for Problem 1c (define (diagonal-duples-iterate-apply n) ; Assume is n a nonnegative integer (iterate-apply 'next-function-goes-here 'done?-function-goes-here 'finalize-function-goes-here 'initial-state-goes-here )) ;;;----------------------------------------------------------------------- ;; Flesh out the following skeleton for Problem 1d (define (diagonal-duples-iter n) (define (outer-tail ...outer-params...) (define (inner-tail ...inter-params...) 'inner-body-goes-here) 'outer-body-goes-here) (outer-tail 'args-go-here)) ;;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ;;; Solo Problem 2: Down and Up Recursion ;;;----------------------------------------------------------------------- ;;; Flesh out the following skeleton of down-and-up-helper for Problem 2a (define (down-and-up nums) (down-and-up-helper 0 nums)) (define (down-and-up-helper sumSoFar ns) (if (null? ns) 'expression1-goes-here 'expression2-goes-here )) ;;;----------------------------------------------------------------------- ;;; Flesh out the following skeleton of down-and-up-foldLR for for Problem 2b (define (down-and-up-foldLR nums) (foldLR 'expression1-goes-here 'expression2-goes-here 'expression3-goes-here 'expression4-goes-here nums)) (define (foldLR combineL state combineR nullfun xs) (if (null? xs) (nullfun state) (let ((next-state (combineL (car xs) state))) (combineR (first xs) next-state (foldLR combineL next-state combineR nullfun (rest xs)))))) ;;;----------------------------------------------------------------------- ;;; Flesh out the following skeletons for Problem 2c (define (my-foldl combine state xs) (foldLR 'expression1-goes-here 'expression2-goes-here 'expression3-goes-here 'expression4-goes-here xs)) (define (my-foldr combine nullval xs) (foldLR 'expression1-goes-here 'expression2-goes-here 'expression3-goes-here 'expression4-goes-here xs)) (define (my-foldr-ternop ternop nullval xs) (foldLR 'expression1-goes-here 'expression2-goes-here 'expression3-goes-here 'expression4-goes-here xs)) ;;;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ;;; Helper Functions from PS5 and iteration lectures (define (genlist-apply next done? keepDoneValue? seed) (if (apply done? seed) (if keepDoneValue? (list seed) null) (cons seed (genlist-apply next done? keepDoneValue? (apply next seed))))) (define (iterate-apply next done? finalize state) (if (apply done? state) (apply finalize state) (iterate-apply next done? finalize (apply next state))))