;; The first three lines of this file were inserted by DrRacket. 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 oct12-dist-part3) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) ; functions are values. ; That is, we can pass them into functions (as arguments), ; and we might even get them back as a result from calling a function. (define l372 (cons 3 (cons 7 (cons 2 '())))) (sort l372 <) (define names (cons "steven" (cons "mark" (cons "ashenafi" (cons "christoper" '()))))) (sort names string>?) ; second-letter boolean ; ASSUME both strings have at least two letters. ; #;(define (second-letter myComp = new Comparator() { public int compare( String strA, String strB ) { return strA.charAt(1) - strB.charAt(1); } }; Arrays.sort( bands, myComp ); System.out.println(Arrays.toString( bands )); // Java 9 provides v.nice syntactic sugar for the above: Arrays.sort( bands, (strA,strB) -> strA.charAt(1) - strB.charAt(1); ); // You can use this short-hand whenever the recipient wants an object // which has only one method. If you provide raw code, Java can figure // out how to make it into an object of a class which has your code // as its one method. (They call it a "functional interface".) System.out.println(Arrays.toString( bands )); Arrays.sort( bands, String::compareTo ); System.out.println(Arrays.toString( bands )); |#