Scheme תרגיל 2

bennyschemeLeave a Comment


#lang racket



(define test (list 2 3))

(define(shapecalc sname paramlist)
  (if (inputCorrect? sname paramlist)
    (if (=(length paramlist) 1)    (list sname (shape1param sname (first paramlist )))

        (if (=(length paramlist) 2)    (list sname (shape2param sname (first paramlist )(first(rest paramlist))))

        (list) )


        ) (list)))
           

(define (shape1param sname a1)
  (cond ((equal? sname 'square) (square a1))
        ((equal? sname 'circle) (circle a1))
        ((equal? sname 'ball) (ball a1))
        ((equal? sname 'cube) (cube a1))  )
 )


(define (shape2param sname a1 a2)
  (cond ((equal? sname 'rectangle) (rectangle a1 a2))
        ((equal? sname 'triangle) (triangle a1 a2))
          )
 )

                               

(define(inputCorrect sname paramlist)
  '#t )

(define (inputCorrect? sname paramlist)
  (cond ((list? paramlist) '#t)
        (else '#f))
  (cond ((and (= (length paramlist) 1)
              (member sname '(square circle ball cube))
              (let ((p (car paramlist))) (number? paramlist))) '#t)
        ((and (= (length paramlist) 2)
              (member sname '(rectangle triangle))
              (let ((p1 (car paramlist)) (p2 (car (cdr paramlist)))) (number? p1) (number? p2))) '#t)
        (else '#f))
  )


; question 2
(define (square prm)
  (* prm prm)  )

(define (circle prm)
  (* (* prm prm) 3.141592654)  )

(define (ball prm)
  (/ (* 4 (* prm (circle prm))) 3)  )


(define (cube prm)
  (* prm (square prm))  )


(define (rectangle p1 p2)
  (* p1 p2)  )


(define (triangle p1 p2)
  (/ (* p1 p2) 2)  )

; question 3 
(define(ballcubediff radius)
 (list (ball radius) (cube radius) (- (ball radius) (cube radius)))
  )

; question 4
(define (cubeballdiff radius)
  (list (cube radius)
        (ball (/ radius 2))
        (- (cube radius) (ball (/ radius 2)) )
        ) )



כתיבת תגובה

האימייל לא יוצג באתר. שדות החובה מסומנים *