sample programs in Scheme

sample programs in Scheme - sample programs in Scheme Kathi...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
;; sample programs in Scheme ;; Kathi Fisler, Jan 12, 2005 ;;----- BASIC FUNCTIONS ----------------------------- ;; circle-perimeter : number -> number ;; computes perimeter of a circle (define (circle-perimeter diam) (* diam pi)) ;; rect-area a;number number -> number ;; computes area of a rectangle (define (rect-area width height) (* width height)) ;; cylinder-surface : number number -> number ;; computes surface area of a cylinder (define (cylinder-surface diam height) (rect-area (circle-perimeter diam) height)) ;; NOTE: in Scheme we tend to write small functions and compose them ;;------ BOOLEANS AND COND --------------------------- ;; between? : number number number -> boolean ;; determine whether first number lies strictly between other two (define (between? num lo hi) (and (< lo num) (< num hi))) ;; sign : number -> string ;; produce word indicating sign of given number (define (sign num) (cond [(> num 0) "positive"] [(< num 0) "negative"] [else "zero"])) ;;------- USER-DEFINED DATATYPES ---------------------- (define-type Shape [circle (radius number?)] [rect (width number?) (height number?)]) ;; Examples of shapes (define circ1 (circle 5)) (define rect1 (rect 10 20)) ;; area : Shape -> number ;; produces area of the given shape
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/06/2008 for the course CS 4536 taught by Professor Fisler during the Winter '08 term at WPI.

Page1 / 4

sample programs in Scheme - sample programs in Scheme Kathi...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online