From 22f3c6b5096b2aefe159face8a8d1fea48365365 Mon Sep 17 00:00:00 2001 From: Matthias Felleisen Date: Wed, 27 Jun 2012 19:02:35 -0400 Subject: [PATCH] added interactive examples up to I/O --- collects/lang/private/advanced-funs.rkt | 315 ++++++++++++++++---- collects/lang/private/beginner-funs.rkt | 7 +- collects/lang/private/intermediate-funs.rkt | 206 +++++++++++-- 3 files changed, 429 insertions(+), 99 deletions(-) diff --git a/collects/lang/private/advanced-funs.rkt b/collects/lang/private/advanced-funs.rkt index 193ab71c0c..c5d0a60bbf 100644 --- a/collects/lang/private/advanced-funs.rkt +++ b/collects/lang/private/advanced-funs.rkt @@ -9,94 +9,279 @@ "../posn.rkt" (for-syntax scheme/base)) -(require "provide-and-scribble.rkt" scribble/manual) +;; Documents the procedures: +(require scribble/manual scribble/eval "sl-eval.rkt" "provide-and-scribble.rkt") (define pp (let ([pretty-print (lambda (v) (pretty-write v))]) pretty-print)) +(define (asl) + (define *bsl + (asl-eval + (require 2htdp/image) + (define c1 (circle 10 "solid" "green")) + + (define zero 0) + + (define one (list 1)) + + (define q (make-posn "bye" 2)) + (define p (make-posn 2 -3)) + + (define s "hello world") + + (define a (list (list 'a 22) (list 'b 8) (list 'c 70))) + (define v (list 1 2 3 4 5 6 7 8 9 'A)) + (define w (list (list (list (list "bye") 3) true) 42)) + (define z (list (list (list (list 'a 'b) 2 3) ) (list false true) "world")) + (define y (list (list (list 1 2 3) false "world"))) + (define x (list 2 "hello" true)))) + (set! asl (lambda () *bsl)) + *bsl) + (provide-and-scribble procedures ("Numbers: Integers, Rationals, Reals, Complex, Exacts, Inexacts" - @defproc[(random [x integer]) integer]{Generates a random natural number less than some given integer, or to generate a random inexact number between 0.0 and 1.0 exclusive.} + @defproc[(random [x integer]) integer]{ + Generates a random natural number less than some given integer + In ASL: @racket[random] generate a random inexact number between 0.0 + and 1.0 exclusive when applied to no argument. + @interaction[#:eval (asl) (random)] + } ) ("Reading and Printing" - @defproc[(with-input-from-file [f string] [p (-> any)]) any]{Opens the named input file and to extract all input from there.} - @defproc[(with-output-to-file [f string] [p (-> any)]) any]{Opens the named output file and to put all output there.} - @defproc[(with-input-from-string [s string] [p (-> any)]) any]{Turns the given string into input for read* operations.} - @defproc[(with-output-to-string [s string] [p (-> any)]) any]{Produces a string from all write/display/print operations.} + @defproc[(with-input-from-file [f string] [p (-> any)]) any]{ + Opens the named input file @racket[f] and allows @racket[p] to read from it. + } + @defproc[(with-output-to-file [f string] [p (-> any)]) any]{ + Opens the named input file @racket[f] and allows @racket[p] to write to it. + } + @defproc[(with-input-from-string [s string] [p (-> any)]) any]{ + Turns @racket[s] into input for @racket[read] operations in @racket[p]. + @interaction[#:eval (asl) + (with-input-from-string "hello" read) + (string-length (symbol->string (with-input-from-string "hello" read)))] + } + @defproc[(with-output-to-string [p (-> any)]) any]{ + Produces a string from all write/display/print operations in @racket[p]. + @interaction[#:eval (asl) + (with-output-to-string (lambda () (display 10)))] + } - @defproc[(print [x any]) void]{Prints the argument as a value to stdout.} - @defproc[(display [x any]) void]{Prints the argument to stdout (without quotes on symbols and strings, etc.).} - @defproc[(write [x any]) void]{Prints the argument to stdout (in a traditional style that is somewhere between print and display).} - @defproc[((pp pretty-print) [x any]) void]{Like write, but with standard newlines and indentation.} - @defproc[(printf [f string] [x any] ...) void]{Formats the rest of the arguments according to the first argument and print it to stdout.} - @defproc[(newline) void]{Prints a newline to stdout.} - @defproc[(read) sexp]{Reads input from the user.}) + @defproc[(print [x any]) void]{ + Prints the argument as a value. + @interaction[#:eval (asl) + (print 10) + (print "hello") + (print 'hello)] + } + @defproc[(display [x any]) void]{ + Prints the argument to stdout (without quotes on symbols and strings, etc.). + @interaction[#:eval (asl) + (display 10) + (display "hello") + (display 'hello)] + } + @defproc[(write [x any]) void]{ + Prints the argument to stdout (in a traditional style that is somewhere between @racket[print] and @racket[display]). + @interaction[#:eval (asl) + (write 10) + (write "hello") + (write 'hello)] + } + @defproc[((pp pretty-print) [x any]) void]{ + Pretty prints S-expressions (like @racket[write]). + @interaction[#:eval (asl) + (pretty-print '((1 2 3) ((a) ("hello world" true) (((false "good bye")))))) + (pretty-print (build-list 10 (lambda (i) (build-list 10 (lambda (j) (= i j)))))) + ] + } + + @defproc[(printf [f string] [x any] ...) void]{ + Formats the rest of the arguments according to the first argument and print it. } + + @defproc[(newline) void]{ + Prints a newline.} + + @defproc[(read) sexp]{ + Reads input from the user.}) ("Lists" - @defproc[(list? [x any]) boolean]{Determines whether some value is a list.} - @defproc[((advanced-list* list*) [x any] ... [l (listof any)]) (listof any)]{Constructs a list by adding multiple items to a list.} - @defproc[((advanced-cons cons) [x X] [l (listof X)]) (listof X)]{Constructs a list.} - @defproc[((advanced-append append) [l (listof any)] ...) (listof any)]{Creates a single list from several.} - @defproc[(assoc [x any] [l (listof any)]) (union (listof any) false)]{Produces the first element on the list whose first is equal? to v; otherwise it produces false.}) + @defproc[(list? [x any]) boolean]{ + Determines whether some value is a list. + } + @defproc[((advanced-list* list*) [x any] ... [l (listof any)]) (listof any)]{ + Constructs a list by adding multiple items to a list. + } + @defproc[((advanced-cons cons) [x X] [l (listof X)]) (listof X)]{ + Constructs a list. + } + @defproc[((advanced-append append) [l (listof any)] ...) (listof any)]{ + Creates a single list from several. + } + @defproc[(assoc [x any] [l (listof any)]) (union (listof any) false)]{ + Produces the first element on the list whose first is equal? to v; otherwise it produces false. + }) ("Misc" - @defproc[(gensym) symbol?]{Generates a new symbol, different from all symbols in the program.} - @defproc[(sleep [sec positive-num]) void]{Causes the program to sleep for the given number of seconds.} - @defproc[(current-milliseconds) exact-integer]{Returns the current “time” in fixnum milliseconds (possibly negative).} - @defproc[(force [v any]) any]{Finds the delayed value; see also delay.} - @defproc[(promise? [x any]) boolean?]{Determines if a value is delayed.} - @defproc[(void) void?]{Produces a void value.} - @defproc[(void? [x any]) boolean?]{Determines if a value is void.}) + @defproc[(gensym) symbol?]{ + Generates a new symbol, different from all symbols in the program. + } + @defproc[(sleep [sec positive-num]) void]{ + Causes the program to sleep for the given number of seconds. + } + @defproc[(current-milliseconds) exact-integer]{ + Returns the current “time” in fixnum milliseconds (possibly negative). + } + @defproc[(force [v any]) any]{ + Finds the delayed value; see also delay. + } + @defproc[(promise? [x any]) boolean?]{ + Determines if a value is delayed. + } + @defproc[(void) void?]{ + Produces a void value. + } + @defproc[(void? [x any]) boolean?]{ + Determines if a value is void. + }) ("Posns" - @defproc[(set-posn-x! [p posn] [x any]) void?]{Updates the x component of a posn.} - @defproc[(set-posn-y! [p posn] [x any]) void]{Updates the y component of a posn.}) + @defproc[(set-posn-x! [p posn] [x any]) void?]{ + Updates the x component of a posn. + } + @defproc[(set-posn-y! [p posn] [x any]) void]{ + Updates the y component of a posn. + }) ("Vectors" - @defproc[(vector [x X] ...) (vector X ...)]{Constructs a vector.} - @defproc[(make-vector [n number] [x X]) (vectorof X)]{Constructs a vector.} - @defproc[(build-vector [n nat] [f (nat -> X)]) (vectorof X)]{Constructs a vector.} - @defproc[(vector-ref [v (vector X)] [n nat]) X]{Extracts an element from a vector.} - @defproc[(vector-length [v (vector X)]) nat]{Determines the length of a vector.} - @defproc[(vector-set! [v (vectorof X)][n nat][x X]) void]{Updates a vector.} - @defproc[(vector->list [v (vectorof X)]) (listof X)]{creates a list of values from the vector of values.} - @defproc[(vector? [x any]) boolean]{Determines if a value is a vector.}) + @defproc[(vector [x X] ...) (vector X ...)]{ + Constructs a vector. + } + @defproc[(make-vector [n number] [x X]) (vectorof X)]{ + Constructs a vector. + } + @defproc[(build-vector [n nat] [f (nat -> X)]) (vectorof X)]{ + Constructs a vector. + } + @defproc[(vector-ref [v (vector X)] [n nat]) X]{ + Extracts an element from a vector. + } + @defproc[(vector-length [v (vector X)]) nat]{ + Determines the length of a vector. + } + @defproc[(vector-set! [v (vectorof X)][n nat][x X]) void]{ + Updates a vector. + } + @defproc[(vector->list [v (vectorof X)]) (listof X)]{ + creates a list of values from the vector of values. + } + @defproc[(vector? [x any]) boolean]{ + Determines if a value is a vector. + }) ("Boxes" - @defproc[(box [x any/c]) box?]{Constructs a box.} - @defproc[(unbox [b box?]) any]{Extracts the boxed value.} - @defproc[(set-box! [b box?][x any/c]) void]{Updates a box.} - @defproc[(box? [x any/c]) boolean?]{Determines if a value is a box.}) + @defproc[(box [x any/c]) box?]{ + Constructs a box. + } + @defproc[(unbox [b box?]) any]{ + Extracts the boxed value. + } + @defproc[(set-box! [b box?][x any/c]) void]{ + Updates a box. + } + @defproc[(box? [x any/c]) boolean?]{ + Determines if a value is a box. + }) ("Hash Tables" - @defproc[((advanced-make-hash make-hash)) (hash X Y)]{Constructs a mutable hash table from an optional list of mappings that uses equal? for comparisions.} - @defproc[((advanced-make-hasheq make-hasheq)) (hash X Y)]{Constructs a mutable hash table from an optional list of mappings that uses eq? for comparisions.} - @defproc[((advanced-make-hasheqv make-hasheqv)) (hash X Y)]{Constructs a mutable hash table from an optional list of mappings that uses eqv? for comparisions.} - @defproc[((advanced-make-immutable-hash make-immutable-hash)) (hash X Y)]{Constructs an immutable hash table from an optional list of mappings that uses equal? for comparisions.} - @defproc[((advanced-make-immutable-hasheq make-immutable-hasheq)) (hash X Y)]{Constructs an immutable hash table from an optional list of mappings that uses eq? for comparisions.} - @defproc[((advanced-make-immutable-hasheqv make-immutable-hasheqv)) (hash X Y)]{Constructs an immutable hash table from an optional list of mappings that uses eqv? for comparisions.} - @defproc[(hash-set! [h (hash X Y)] [k X] [v Y]) void?]{Updates a mutable hash table with a new mapping.} - @defproc[(hash-set [h (hash X Y)] [k X] [v Y]) (hash X Y)]{Constructs an immutable hash table with one new mapping from an existing immutable hash table.} - @defproc[(hash-ref [h (hash X Y)] [k X]) Y]{Extracts the value associated with a key from a hash table; the three argument case allows a default value or default value computation.} - @defproc[(hash-ref! [h (hash X Y)] [k X] [v Y]) Y]{Extracts the value associated with a key from a mutable hash table; if the key does not have an mapping, the third argument is used as the value (or used to compute the value) and is added to the hash table associated with the key.} - @defproc[(hash-update! [h (hash X Y)] [k X] [f (Y -> Y)]) void?]{Composes hash-ref and hash-set! to update an existing mapping; the third argument is used to compute the new mapping value; the fourth argument is used as the third argument to hash-ref.} - @defproc[(hash-update [h (hash X Y)] [k X] [f (Y -> Y)]) (hash X Y)]{Composes hash-ref and hash-set to update an existing mapping; the third argument is used to compute the new mapping value; the fourth argument is used as the third argument to hash-ref.} - @defproc[(hash-has-key? [h (hash X Y)] [x X]) boolean]{Determines if a key is associated with a value in a hash table.} - @defproc[(hash-remove! [h (hash X Y)] [x X]) void]{Removes an mapping from a mutable hash table.} - @defproc[(hash-remove [h (hash X Y)] [k X]) (hash X Y)]{Constructs an immutable hash table with one less mapping than an existing immutable hash table.} - @defproc[(hash-map [h (hash X Y)] [f (X Y -> Z)]) (listof Z)]{Constructs a new list by applying a function to each mapping of a hash table.} - @defproc[(hash-for-each [h (hash X Y)] [f (X Y -> any)]) void?]{Applies a function to each mapping of a hash table for effect only.} - @defproc[(hash-count [h hash]) integer]{Determines the number of keys mapped by a hash table.} - @defproc[(hash-copy [h hash]) hash]{Copies a hash table.} - @defproc[(hash? [x any]) boolean]{Determines if a value is a hash table.} - @defproc[(hash-equal? [h hash?]) boolean]{Determines if a hash table uses equal? for comparisons.} - @defproc[(hash-eq? [h hash]) boolean]{Determines if a hash table uses eq? for comparisons.} - @defproc[(hash-eqv? [h hash]) boolean]{Determines if a hash table uses eqv? for comparisons.})) - - - + @defproc[((advanced-make-hash make-hash)) (hash X Y)]{ + Constructs a mutable hash table from an optional list of mappings that + uses equal? for comparisions. + } + @defproc[((advanced-make-hasheq make-hasheq)) (hash X Y)]{ + Constructs a mutable hash table from an optional list of mappings that + uses eq? for comparisions. + } + @defproc[((advanced-make-hasheqv make-hasheqv)) (hash X Y)]{ + Constructs a mutable hash table from an optional list of mappings that + uses eqv? for comparisions. + } + @defproc[((advanced-make-immutable-hash make-immutable-hash)) (hash X Y)]{ + Constructs an immutable hash table from an optional list of mappings + that uses equal? for comparisions. + } + @defproc[((advanced-make-immutable-hasheq make-immutable-hasheq)) (hash X Y)]{ + Constructs an immutable hash table from an optional list of mappings + that uses eq? for comparisions. + } + @defproc[((advanced-make-immutable-hasheqv make-immutable-hasheqv)) (hash X Y)]{ + Constructs an immutable hash table from an optional list of mappings + that uses eqv? for comparisions. + } + @defproc[(hash-set! [h (hash X Y)] [k X] [v Y]) void?]{ + Updates a mutable hash table with a new mapping. + } + @defproc[(hash-set [h (hash X Y)] [k X] [v Y]) (hash X Y)]{ + Constructs an immutable hash table with one new mapping from an + existing immutable hash table. + } + @defproc[(hash-ref [h (hash X Y)] [k X]) Y]{ + Extracts the value associated with a key from a hash table; the three + ; argument case allows a default value or default value computation. + } + @defproc[(hash-ref! [h (hash X Y)] [k X] [v Y]) Y]{ + Extracts the value associated with a key from a mutable hash table; if + ; the key does not have an mapping, the third argument is used as the + ; value (or used to compute the value) and is added to the hash table + ; associated with the key. + } + @defproc[(hash-update! [h (hash X Y)] [k X] [f (Y -> Y)]) void?]{ + Composes hash-ref and hash-set! to update an existing mapping; the + ; third argument is used to compute the new mapping value; the fourth + ; argument is used as the third argument to hash-ref. + } + @defproc[(hash-update [h (hash X Y)] [k X] [f (Y -> Y)]) (hash X Y)]{ + Composes hash-ref and hash-set to update an existing mapping; the third + ; argument is used to compute the new mapping value; the fourth + ; argument is used as the third argument to hash-ref. + } + @defproc[(hash-has-key? [h (hash X Y)] [x X]) boolean]{ + Determines if a key is associated with a value in a hash table. + } + @defproc[(hash-remove! [h (hash X Y)] [x X]) void]{ + Removes an mapping from a mutable hash table. + } + @defproc[(hash-remove [h (hash X Y)] [k X]) (hash X Y)]{ + Constructs an immutable hash table with one less mapping than an + existing immutable hash table. + } + @defproc[(hash-map [h (hash X Y)] [f (X Y -> Z)]) (listof Z)]{ + Constructs a new list by applying a function to each mapping of a hash + table. + } + @defproc[(hash-for-each [h (hash X Y)] [f (X Y -> any)]) void?]{ + Applies a function to each mapping of a hash table for effect only. + } + @defproc[(hash-count [h hash]) integer]{ + Determines the number of keys mapped by a hash table. + } + @defproc[(hash-copy [h hash]) hash]{ + Copies a hash table. + } + @defproc[(hash? [x any]) boolean]{ + Determines if a value is a hash table. + } + @defproc[(hash-equal? [h hash?]) boolean]{ + Determines if a hash table uses equal? for comparisons. + } + @defproc[(hash-eq? [h hash]) boolean]{ + Determines if a hash table uses eq? for comparisons. + } + @defproc[(hash-eqv? [h hash]) boolean]{ + Determines if a hash table uses eqv? for comparisons. + })) #| @defproc[(random (case-> (integer -> integer) (-> (and/c real inexact? (>/c 0) ( Z)] [l (listof X)] ...) (listof Z)]{Constructs a new list by applying a function to each item on one or more existing lists.} - @defproc[(for-each [f (any ... -> any)] [l (listof any)] ...) void?]{Applies a function to each item on one or more lists for effect only.} - @defproc[((intermediate-filter filter) [p? (X -> boolean)] [l (listof X)]) (listof X)]{Constructs a list from all those items on a list for which the predicate holds.} - @defproc[((intermediate-foldr foldr) [f (X Y -> Y)] [base Y] [l (listof X)]) Y]{(foldr f base (list x-1 ... x-n)) = (f x-1 ... (f x-n base))} - @defproc[((intermediate-foldl foldl) [f (X Y -> Y)] [base Y] [l (listof X)]) Y]{(foldl f base (list x-1 ... x-n)) = (f x-n ... (f x-1 base))} - @defproc[(build-list [n nat] [f (nat -> X)]) (listof X)]{(build-list n f) = (list (f 0) ... (f (- n 1)))} - @defproc[((intermediate-build-string build-string) [n nat] [f (nat -> char)]) string]{(build-string n f) = (string (f 0) ... (f (- n 1)))} - @defproc[((intermediate-quicksort quicksort) [l (listof X)] [comp (X X -> boolean)]) (listof X)]{Constructs a list from all items on a list in an order according to a predicate.} - @defproc[((intermediate-sort sort) [l (listof X)] [comp (X X -> boolean)]) (listof X)]{Constructs a list from all items on a list in an order according to a predicate.} - @defproc[((intermediate-andmap andmap) [p? (X -> boolean)] [l (listof X)]) boolean]{(andmap p (list x-1 ... x-n)) = (and (p x-1) ... (p x-n))} - @defproc[((intermediate-ormap ormap) [p? (X -> boolean)] [l (listof X)]) boolean]{(ormap p (list x-1 ... x-n)) = (or (p x-1) ... (p x-n))} - @defproc[(argmin [f (X -> real)] [l (listof X)]) X]{Finds the (first) element of the list that minimizes the output of the function.} - @defproc[(argmax [f (X -> real)] [l (listof X)]) X]{Finds the (first) element of the list that maximizes the output of the function.} - @defproc[(memf [p? (X -> any)] [l (listof X)]) (union false (listof X))]{Produces false if the predicate given as the first argument produces false for all items on the given list. If the given predicate produces true for any of the items on the list, memf returns the sub-list starting from that element.} - @defproc[(apply [f (X-1 ... X-N -> Y)] [x-1 X-1] ... [l (list X-i+1 ... X-N)]) Y]{Applies a function using items from a list as the arguments.} - @defproc[(compose [f (X -> Y)] [g (Y -> Z)]) (X -> Z)]{Composes a sequence of procedures into a single procedure.} - @defproc[(procedure? [x any]) boolean?]{Produces true if the value is a procedure.})) + ("Higher-Order Functions" + @defproc[(map [f (X ... -> Z)] [l (listof X)] ...) (listof Z)]{ + Constructs a new list by applying a function to each item on one or more existing lists: + @codeblock{(map f (list x-1 ... x-n)) = (list (f x-1) ... (f x-n))} + @interaction[#:eval (isl) + (map add1 '(3 -4.01 2/5)) + (map (lambda (x) (list 'my-list (+ x 1))) '(3 -4.01 2/5))] + } + @defproc[(for-each [f (any ... -> any)] [l (listof any)] ...) void?]{ + Applies a function to each item on one or more lists for effect only: + @codeblock{(for-each f (list x-1 ... x-n)) = (begin (f x-1) ... (f x-n))} + @interaction[#:eval (asl-eval) + (for-each (lambda (x) (begin (display x) (newline))) '(1 2 3)) + ] + } + @defproc[((intermediate-filter filter) [p? (X -> boolean)] [l (listof X)]) (listof X)]{ + Constructs a list from all those items on a list for which the predicate holds. + @interaction[#:eval (isl) + (filter odd? '(0 1 2 3 4 5 6 7 8 9)) + threshold + (filter (lambda (x) (>= x threshold)) '(0 1 2 3 4 5 6 7 8 9)) + ] + } + @defproc[((intermediate-foldr foldr) [f (X Y -> Y)] [base Y] [l (listof X)]) Y]{ + @codeblock{(foldr f base (list x-1 ... x-n)) = (f x-1 ... (f x-n base))} + @interaction[#:eval (isl) + (foldr + 0 '(0 1 2 3 4 5 6 7 8 9)) + a-list + (foldr (lambda (x r) (if (> x threshold) (cons (* 2 x) r) r)) '() a-list) + ] + } + @defproc[((intermediate-foldl foldl) [f (X Y -> Y)] [base Y] [l (listof X)]) Y]{ + @codeblock{(foldl f base (list x-1 ... x-n)) = (f x-n ... (f x-1 base))} + @interaction[#:eval (isl) + (foldl + 0 '(0 1 2 3 4 5 6 7 8 9)) + a-list + (foldl (lambda (x r) (if (> x threshold) (cons (* 2 x) r) r)) '() a-list) + ] + } + @defproc[(build-list [n nat] [f (nat -> X)]) (listof X)]{ + Constructs a list by applying @racket[f] to the numbers between @racket[0] and @racket[(- n 1)]: + @codeblock{(build-list n f) = (list (f 0) ... (f (- n 1)))} + @interaction[#:eval (isl) + (build-list 22 add1) + i + (build-list 3 (lambda (j) (+ j i))) + (build-list 5 + (lambda (i) + (build-list 5 + (lambda (j) + (if (= i j) 1 0))))) + ] + } + @defproc[((intermediate-build-string build-string) [n nat] [f (nat -> char)]) string]{ + Constructs a string by applying @racket[f] to the numbers between @racket[0] and @racket[(- n 1)]: + @codeblock{(build-string n f) = (string (f 0) ... (f (- n 1)))} + @interaction[#:eval (isl) + (build-string 10 integer->char) + (build-string 26 (lambda (x) (integer->char (+ 65 x))))] + } + @defproc[((intermediate-quicksort quicksort) [l (listof X)] [comp (X X -> boolean)]) (listof X)]{ + Sorts the items on @racket[l], in an order according to @racket[comp] (using the quicksort algorithm). + @interaction[#:eval (isl) + (quicksort '(6 7 2 1 3 4 0 5 9 8) <)] + } + @defproc[((intermediate-sort sort) [l (listof X)] [comp (X X -> boolean)]) (listof X)]{ + Sorts the items on @racket[l], in an order according to @racket[comp]. + @interaction[#:eval (isl) + (sort '(6 7 2 1 3 4 0 5 9 8) <)] + } + @defproc[((intermediate-andmap andmap) [p? (X -> boolean)] [l (listof X)]) boolean]{ + Determines whether @racket[p?] holds for all items of @racket[l]: + @codeblock{(andmap p (list x-1 ... x-n)) = (and (p x-1) ... (p x-n))} + @interaction[#:eval (isl) + (andmap odd? '(1 3 5 7 9)) + threshold + (andmap (lambda (x) (< x threshold)) '(0 1 2)) + (andmap even? '()) + ] + } + @defproc[((intermediate-ormap ormap) [p? (X -> boolean)] [l (listof X)]) boolean]{ + Determines whether @racket[p?] holds for at least one items of @racket[l]: + @codeblock{(ormap p (list x-1 ... x-n)) = (or (p x-1) ... (p x-n))} + @interaction[#:eval (isl) + (ormap odd? '(1 3 5 7 9)) + threshold + (ormap (lambda (x) (< x threshold)) '(6 7 8 1 5)) + (ormap even? '()) + ] + } + @defproc[(argmin [f (X -> real)] [l (listof X)]) X]{ + Finds the (first) element of the list that minimizes the output of the function. + @interaction[#:eval (isl) + (argmin second '((sam 98) (carl 78) (vincent 93) (asumu 99))) + ] + } + @defproc[(argmax [f (X -> real)] [l (listof X)]) X]{ + Finds the (first) element of the list that maximizes the output of the function. + @interaction[#:eval (isl) + (argmax second '((sam 98) (carl 78) (vincent 93) (asumu 99))) + ] + } + @defproc[(memf [p? (X -> any)] [l (listof X)]) (union false (listof X))]{ + Produces @racket[false] if @racket[p?] produces @racket[false] for all + items on @racket[l]. If @racket[p?] produces @racket[true] for any of + the items on @racket[l], @racket[memf] returns the sub-list starting + from that item. + @interaction[#:eval (isl) + (memf odd? '(2 4 6 3 8 0)) + ] + } + @defproc[(apply [f (X-1 ... X-N -> Y)] [x-1 X-1] ... [l (list X-i+1 ... X-N)]) Y]{ + Applies a function using items from a list as the arguments: + @codeblock{(apply f (list x-1 ... x-n)) = (f x-1 ... x-n)} + @interaction[#:eval (isl) + a-list + (apply max a-list) + ] + } + @defproc[(compose [f (X -> Y)] [g (Y -> Z)]) (X -> Z)]{ + Composes a sequence of procedures into a single procedure: + @codeblock{(compose f g) = (lambda (x) (f (g x)))} + @interaction[#:eval (isl) + ((compose add1 second) '(add 3)) + (map (compose add1 second) '((add 3) (sub 2) (mul 4))) + ] + } + @defproc[(procedure? [x any]) boolean?]{ + Produces true if the value is a procedure. + @interaction[#:eval (isl) + (procedure? cons) + (procedure? add1) + (procedure? (lambda (x) (> x 22))) + ] + } + ) + )