#lang racket ;; cse 413 12au lecture 3 sample functions ;; note: some of these functions like len and app are reimplementations ;; of ones in the standard library to demonstrate how these are implemented. ;; use the library versions in your own code. ;; = larger value of a or b (define (biggest a b) (if (> a b) a b)) ;; same as above using cond instead of if (define (bigger a b) (cond ((> a b) a) (else b))) ;; = description of the weather depending on the temperature ;; (uses cond for multi-way decision) (define (weather temp) (cond ((> temp 80) 'warm) ((< temp 60) 'cold) (else 'seattle))) ;; = length of list lst (define (len lst) (if (null? lst) 0 (+ 1 (len (cdr lst))))) ;; = sum of items in list lst, which must be numbers (define (lstsum lst) (if (null? lst) 0 (+ (car lst) (lstsum (cdr lst))))) ;; = sum of numbers in list lst. non-numeric list elements are ignored. (define (numsum lst) (cond ((null? lst) 0) ((number? (car lst)) (+ (car lst) (numsum (cdr lst)))) (else (numsum (cdr lst))))) ;; = list with contents of lst1 followed by contents of lst2 (define (app lst1 lst2) (if (null? lst1) lst2 (cons (car lst1) (app (cdr lst1) lst2))))