# a3q2 - ;Examples(ith-smallest 1(3 5 2 4 1 =>...

This preview shows page 1. Sign up to view the full content.

;; The first three lines of this file were inserted by DrScheme. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname a3q2) (read-case- sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) ;;cs116 Assignment 3 Question 2 ;; r46wang 20319247 ;;ith-smallest: int [length(list inputted)>= int >=1] (listotf num)[nonempty] -> num ;; consumes a positive integer and a list of numbers and returns the number at the position indicated by the integer from the inputted list if the inputted list was sorted from smallest to largest. (returns the ith smallest element from the list)
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ;;Examples: ;;(ith-smallest 1 '(3 5 2 4 1)) => 1 ;;(ith-smallest 2 '(3 5 2 4 1)) => 2 ;;(ith-smallest 5 '(3 5 2 4 1)) => 5 (define (ith-smallest i alon) (local [;; list-min: (listof num)[nonempty] -> num ;; produces the smallest value of the list inputted. (define (list-min lon) (foldr min (first lon) lon))] (cond [(= i 1)(list-min alon)] [else (ith-smallest (- i 1) (filter (lambda (x)(not (= (list-min alon) x))) alon))]))) ;;Tests: (check-expect (ith-smallest 1 '(3 5 2 4 1)) 1) (check-expect (ith-smallest 2 '(3 5 2 4 1)) 2) (check-expect (ith-smallest 5 '(3 5 2 4 1)) 5)...
View Full Document

## This note was uploaded on 10/21/2010 for the course CS 116 taught by Professor T.vasiga during the Fall '09 term at Waterloo.

Ask a homework question - tutors are online