01202010_recursive - (if (empty? list) false (or (pred...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
;; 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-reader.ss" "lang")((modname 01202010_recursive) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating- decimal #f #t none #f ()))) ; For recursive ; 1. Test if base case(s) ; 2. Otherwise ; - branch apart ; - recursively process parts ; - put back together (define (any-zero list) (if (empty? list) false (or (= (first list) 0) (any-zero (rest list) ) ) ) ) ; copy and change the number, which is bad (define (any-one list) (if (empty? list) false (or (= (first list) 1) (any-one (rest list) ) ) ) ) ; it's better to pass parameter ;(define (any-equals num list) ; (if (empty? list) ; (or (= (first list) num) ; (any-equals num (rest list)) ; ) ; ) ; ) (define (any pred list) ;; only works for intermediate student language and higher
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: (if (empty? list) false (or (pred (first list)) (any pred (rest list))) ) ) ;(define (any-zero list) (any zero? list)) ;(define (any-one list) (any is-one list)) (define (any-positive list) (any positive? list)) ;** can pass function as parameter and use it ; (filter positive? my-list) ; filter gives list of all the ones which that predicate is true (define (inc x) (+ x 1)) (define (square x) (* x x)) (define (positive x) (> x 0)) (define (negative x) (< x 0)) (define my-list (list 1 -2 3 -4 5 -6 7)) (define (all pred list) (= (length list) (length (filter pred list)))) (map inc (map square my-list)) (foldl + 0 my-list) (foldl + 0 (map square my-list)) (define string-list (list "hello" "goodbye" "caio" "aloha")) (foldr string-append "" string-list) (foldl string-append "" string-list)...
View Full Document

Page1 / 2

01202010_recursive - (if (empty? list) false (or (pred...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online