#lang racket ;; CSE 413 19wi Lecture 2 ;; Sample code ;; Return 2*n (define (twice n) (* 2 n)) ;; Return 2*n - version with explicit lambda (define double (lambda (n) (* 2 n))) ;; Bind name to numeric value (define xvii 17) ;; Functions with conditionals (if and cond) (define (too-cold temp) (if (< temp 65) #t #f)) (define (weather temp) (if (too-cold temp) "brrr" "ahhhh")) (define (comfort temp) (cond [(> temp 80) 'too-hot] [(> temp 60) 'nice] [(< temp 40) 'too-cold] [else 'seattle])) ;; = n! (define (fact n) (if (< n 2) 1 (* n (fact (- n 1))))) ;; a couple of lists for examples below (define nums '(3 5 12 -4 17)) (define pies '(apple banana cherry)) ;; return the sum of the items in a list of numbers (define (sum lst) (if (null? lst) 0 (+ (car lst) (sum (cdr lst))))) ;; return the sum of the numbers in lst ignoring other values (define (numsum lst) (cond [(null? lst) 0] [(number? (car lst)) (+ (car lst) (numsum (cdr lst)))] [else (numsum (cdr lst))])) ;; return the number of items in lst (define (len lst) (if (null? lst) 0 (+ 1 (len (cdr lst))))) ;; return a list with ys appended after xs (define (app xs ys) (if (null? xs) ys (cons (car xs) (app (cdr xs) ys))))