Source

racketeering / a2functions.scm

Full commit
#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.