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

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

;; 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 (define (area ashape)
Image of page 1

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

Image of page 2
This is the end of the preview. Sign up to access the rest of the document.
  • Winter '08
  • Fisler
  • Height, bintree

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern