Commits

Steven! Ragnarök committed bf0c944

Initial commit.

test.scm is Copyright 2011 Dr. Jeffrey Smith, SJSU
and may not be used without explicit permission.

a2functions.scm is Copyright 2011 Steven! Ragnarök
and is made available under the terms of the GNU
GPLv3.

Comments (0)

Files changed (2)

+#lang racket
+; CS152 Assignment Two
+
+; 1) (10 Points) Define a function p to compute the polynomial
+; p(x) = 1 + x + x^2/2 + x^3/6.
+; Note that this polynomial approximates the function e^x.
+; You needn't compute the function in the most efficient possible way.
+(define (p x) (+ 1.0 x (/ (* x x) 2.0) (/ (* x x x) 6.0)))
+
+; 2) (15 points) Define a function symbol-append that takes two arguments.
+; If both arguments are symbols, the function is to return a symbol
+; representing the concatenation of the two symbols. Otherwise the function
+; is to return #f. So for example (symbol-append 'hello 'there) should return
+; the symbol hellothere.
+(define (symbol-append a b)
+  (if
+   (and
+    (symbol? a)
+    (symbol? b))
+	 (string->symbol (string-append (symbol->string a) (symbol->string b)))
+	 #f))
+
+; 3) (15 points) Define a predicate list-of-nonempty-strings? of one argument
+; that returns #t if and only if its argument is a list and every member of
+; the list is a nonempty string.
+(define (list-of-nonempty-strings? lst)
+	(and
+		(and
+			(string? (car lst))
+			(> (string-length (car lst)) 0))
+		(or
+			(null? (cdr lst))
+			(list-of-nonempty-strings? (cdr lst)))))
+
+; Define a function first-string that takes a list of nonempty strings and
+; returns the first string in case-insensitive lexicographical order.
+; If the list is empty or is not a list of strings, you are to return the
+; empty string.
+(define (first-string lst)
+	(define (radix-sort-string-list lst)
+		(define (string-list->char-list lst)
+			(if
+				(null? lst)
+				'()
+				(append
+					(list (string->list (car lst)))
+					(string-list->char-list (cdr lst))))
+		(if
+			(list-of-nonempty-strings? lst)
+			(car (radix-sort-string-list lst))
+			'()))
+
+	; (10 points) Define a function longest-satisfying-prefix that takes a list and a unary predicate as argument and returns a copy of the input list, up to but not including the first member that fails to satisfy the predicate. So for example (longest-satisfying-prefix '(16 8 4 2 1) even?) should return (16 8 4 2), as should (longest-satisfying-prefix '(16 8 4 2) even?).
+
+	; (10 points) Define a boolean-valued function alist-simple? that takes an association list, and returns #t if and only if all of the members of the assocation list have distinct cars. That is, the function is to return #f precisely when the association list can be replaced by a shorter equivalent association list.
+#lang racket
+;  The test function -- prints each test case and its value on 
+;    separate lines
+
+(define (test)
+  (define (helper test-cases)
+    (for-each
+     (lambda (test-case)
+       (with-handlers (((lambda (exc) (string? exc))
+                        (lambda (exc) (display exc) (newline)))
+                       ((lambda (exc) (and (exn:fail? exc) ))
+                        (lambda (exc) 
+                          (display (exn-message exc)) (newline))))
+         (print test-case) (newline)
+         (print (eval test-case)) (newline)))
+     test-cases)
+    (newline))
+  (helper test-cases))
+   
+   
+   
+;;;;;;;;;;;;;;;;;;;;;;; TEST CASES ;;;;;;;;;;;;;;;;;;;;;;
+
+(define test-cases
+  '(
+    (p 1)
+    (p 2)
+    (p 0.5)
+    (p 10)
+    (list (p 0.1) (exp 0.1))
+    (p null)
+    
+    (symbol-append 'hello 'there)
+    (symbol-append 'CS '_152)
+    (symbol-append "hello" 'there)
+    (symbol-append 'CS 152)    
+    
+    (symbol-append-with-exceptions 'hello 'there)
+    (symbol-append-with-exceptions 'CS '_152)
+    (symbol-append-with-exceptions "hello" 'there)
+    (symbol-append-with-exceptions 'CS 152)    
+    
+    (list-of-nonempty-strings? null)
+    (list-of-nonempty-strings? #t)    
+    (list-of-nonempty-strings? '("I" "like" "CS" 152))
+    (list-of-nonempty-strings? '("I" "like" "CS" "152"))
+    (list-of-nonempty-strings? '("I" "like" "CS" "152" ""))
+    (list-of-nonempty-strings? '("" "like" "CS" "152"))
+    (list-of-nonempty-strings? '(I "like" "CS" "152"))   
+    (list-of-nonempty-strings? '("I" "like" ("CS" "152") "a" "lot"))
+    
+    (first-string null)
+    (first-string #t)    
+    (first-string '("I" "like" "CS" 152))
+    (first-string '("I" "like" "CS" "152"))
+    (first-string '("I" "like" "CS" "152" ""))
+    (first-string '("" "like" "CS" "152"))
+    (first-string '(I "like" "CS" "152"))   
+    (first-string '("I" "like" ("CS" "152") "a" "lot"))
+    (first-string '("one" "two" "three" "four")) 
+    (first-string '("five" "six" "seven"))
+    (first-string '("Zero" "one" "two" "three" "four" "five" "six"))
+
+    (longest-satisfying-prefix '(16 8 4 2 1) even?)
+    (longest-satisfying-prefix '(16 8 4 2) even?)
+    (longest-satisfying-prefix '(1 2 3 4 5) odd?)
+    (longest-satisfying-prefix '(2 3 4 5) odd?)
+    (longest-satisfying-prefix '(1 3 5 7 9) odd?)    
+    (longest-satisfying-prefix '(1 2 0.5 10) 
+                               (lambda (x) (and (>= x 1) (< x 10))))
+    (longest-satisfying-prefix '("I" "like" "CS" 152) string?)
+    
+    (alist-simple? '((a b) (c d) (e f)))
+    (alist-simple? '())
+    (alist-simple? '((4 2)))
+    (alist-simple? '((4 2) (1 1) (0 0) (1 -1)))
+    (alist-simple? '((1 1) (4 2) (0 0) (1 -1)))
+    (alist-simple? '((4 2) (0 0) (14 -1) (14 1)))
+    (alist-simple? '((4 2) (1 1) (0 0) (-1 1)))
+    
+))