scheme תרגיל 3

bennyschemeLeave a Comment

#lang racket


(define COUNT 0)

(define date '(12 jan 1989))
(define listnum '(34 120 25))

(define months '((jan 31)(feb (28 29)) (mar 31) (apr 30) (may 31) (jun 30) (jul 31) (aug 31) (sep 30) (oct 31) (nov 30) (dec 31)))


(define (dayscount taarich)
  (if(list? taarich)
     (if(and(= (length taarich) 3) (assoc (cadr taarich) months) )
        (let((year (caddr taarich)))
          (if(or(and( = (modulo year 4) 0)not(=(modulo year 100)0))(=(modulo year 400) 0))
             (calculatedays months taarich  #t)
             (calculatedays months taarich  #f)))
         (error "error input"))
      (error "error input")))
   



(define (calculatedays monthsList taarich boolMeuberet)
(cond((equal? (caar monthsList) (cadr taarich))(car taarich))
     ((and (equal?(caar monthsList) 'feb)boolMeuberet(not(equal?(cadr taarich) 'feb))) (+( cadar(cdar monthsList))(calculatedays (cdr monthsList) taarich boolMeuberet)))
     ((equal?(caar monthsList) 'feb) (+(caadar monthsList) (calculatedays (cdr monthsList) taarich boolMeuberet)))
       ((not (equal? (cadr taarich) (caar monthsList))) (+ (cadar monthsList)(calculatedays (cdr monthsList) taarich boolMeuberet )))




      
 )) 


;input correct for question 2
(define(inputCorrect2? NUM)
  (if    (> NUM 0)

         (if(> NUM 0) '#t '#f) '#f))   
       
      ;((list? paramlist) '#t)    (else '#f))
       ; ((if(integerp NUM) '#t) (else  '#f)) '#f) 
;inputCorrect2



(define (checknum N)
(if(inputCorrect2? N)
   (cond (  (and  ( = (modulo N 3) 0  ) (= (modulo N 5) 0  )   (= (modulo N 7) 0)  )          '( Bim Bam Bum) )
      (  (and  ( = (modulo N 3) 0  ) (not(= (modulo N 5) 0  ))  (not (= (modulo N 7) 0)  ))          '( Bim ) )
      (  (and  ( = (modulo N 5) 0  ) (not(= (modulo N 3) 0  ))  (not (= (modulo N 7) 0)  ))          '( Bam ) )
      (  (and  ( = (modulo N 7) 0  ) (not(= (modulo N 3) 0  ))  (not (= (modulo N 5) 0)  ))          '( Bum ) )
      (  (and  ( = (modulo N 3) 0  ) (= (modulo N 5) 0  )   (not(= (modulo N 7) 0)  ))          '( BimBam) )
      (  (and  ( = (modulo N 5) 0  ) (= (modulo N 7) 0  )   (not(= (modulo N 3) 0)  ))          '( BamBum) )
      (  (and  ( = (modulo N 3) 0  ) (= (modulo N 7) 0  )   (not(= (modulo N 5) 0)  ))          '( BimBum) )
      )
 "error input"
  ))


;question 3


; the input chac function
(define(middlenum L)
(if(list? L)
   (if (= (length L) 3)
         (if   (or (= (car L) (cadr L)  )  (= (car L) (caddr L)  )  (= (cadr L) (caddr L)  )    ) "error input" (middlenumCalc L))
 
  "les then 3 elements in the list ")
   "the input must be a list")
  )
; the main function
(define ( middlenumCalc L)
(cond(  (or(and  (< (car L) (cadr L)  )  (> (car L) (caddr L)  ) )  (and  (> (car L) (cadr L)  )  (< (car L) (caddr L)  )  ) )   (car L)   )
     (  (or(and  (< (cadr L) (car L)  )  (> (cadr L) (caddr L)  ) )  (and  (> (cadr L) (car L)  )  (< (cadr L) (caddr L)  )  ) )   (cadr L)   )
     (  (or(and  (< (caddr L) (car L)  )  (> (caddr L) (cadr L)  ) )  (and  (> (caddr L) (car L)  )  (< (caddr L) (cadr L)  )  ) )   (caddr L)   )
     
     )
  )

;question 4
(define(num2digit N)
( middlenumCalc  (list (modulo N 10)(modulo(truncate(/ N 10))10)   ( truncate(/(truncate(/ N 10))10))  )) 
                  )









benny & david


#lang racket

(define months '((jan 31) (feb (28 29)) (mar 31) (apr 30) (may 31) (jun 30) (jul 31) (aug 31) (sep 30) (oct 31) (nov 30) (dec 31)))

(define (mounth? mo)
  (if (and (symbol? mo) (or (equal? mo 'jan) (equal? mo 'feb) (equal? mo 'mar) (equal? mo 'apr) (equal? mo 'may) (equal? mo 'jun) (equal? mo 'jul) (equal? mo 'aug) (equal? mo 'sep) (equal? mo 'oct) (equal? mo 'nov) (equal? mo 'dec)))
      #t
      #f
  )
)

(define (integer/ x y)
  (/ (- x (modulo x y)) y)
)

(define (minus mon)
  (cond
    ((equal? mon 'dec)
    'nov)
    ((equal? mon 'nov)
    'oct)
    ((equal? mon 'oct)
    'sep)
    ((equal? mon 'sep)
    'aug)
    ((equal? mon 'aug)
    'jul)
    ((equal? mon 'jul)
    'jun)
    ((equal? mon 'jun)
    'may)
    ((equal? mon 'may)
    'apr)
    ((equal? mon 'apr)
    'mar)
    ((equal? mon 'mar)
    'feb)
    ((equal? mon 'feb)
    'jan)
    (#t
    'non)
  )
)

(define (daysinamonth mounth year)
  (cond
    ((equal? mounth 'feb)
    (if  (or (and (= (modulo year 4) 0) (not (= (modulo year 100) 0))) (= (modulo year 400) 0))
         (cadr (cadar (cdr months)))
         (car (cadar (cdr months)))
    ))
    (else (cadr (assoc mounth months)))
  )
)

(define (dayscount taarich [result 0])
  (cond
    ((not (list? taarich))
    "this is not a list")
    ((not (= (length taarich) 3))
    "the list length is not 3")
    (else (let ((day (car taarich)) (mon (cadr taarich)) (yea (caddr taarich)))
            (cond
              ((equal? mon 'non)
               result)
              ((not (and (number? day) (> day 0) (mounth? mon) (number? yea) ))
              "the list is not a rightful taarich")
              ((= result 0)
              (dayscount (list day (minus mon) yea) (+ result day)))
              ((> result 0)
              (dayscount (list day (minus mon) yea) (+ result (daysinamonth mon yea))))
              (#t "ERROR")
             )
           )
    )
  )
)

(define (checknum N)
  (cond
    ((not (and (integer? N) (> N 0)))
    "N is not what you where asked to put in this function")
    ((= (modulo N (* 3 5 7)) 0)
    '(Bim Bam Bum))
    ((and (= (modulo N (* 3 5)) 0) (not (= (modulo N 7) 0)))
    '(Bim Bam))
    ((and (= (modulo N (* 5 7)) 0) (not (= (modulo N 3) 0)))
    '(Bam Bum))
    ((and (= (modulo N (* 7 3)) 0) (not (= (modulo N 5) 0)))
    '(Bim Bum))
    ((and (= (modulo N 3) 0) (not (or (= (modulo N 5) 0) (= (modulo N 7) 0))))
    '(Bim))
    ((and (= (modulo N 5) 0) (not (or (= (modulo N 7) 0) (= (modulo N 3) 0))))
    '(Bam))
    ((and (= (modulo N 7) 0) (not (or (= (modulo N 3) 0) (= (modulo N 5) 0))))
    '(Bum))
    (#t
    "N does not divide by 3 or 5 or 7 whithout a remainder")
  )
)

(define (middlenum L)
  (if (or (not (list? L)) (not (= (length L) 3)))
      "L is not a list in the size of 3"
      (let ((f (car L)) (s (cadr L)) (t (caddr L)))
        (cond
          ((not (and (number? f) (number? s) (number? t)))
          "The list does not contain 3 numbers")
          (#t
          (min (max t (min f s)) (max f s)))
        )
      )
  )
)

(define (num2digit N)
  (if (not (and (integer? N) (>= N 100) (< N 1000)))
    ("N is not an integer with 3 digits")
    (middlenum (list (integer/ N 100) (modulo (integer/ N 10) 10) (modulo N 10)))
  )
)

H.s

פיתרון 3

(define months '((jan . 31) (feb (28 . 29)) (mar . 31) (apr . 30) (may . 31) (jun . 30) (jul . 31) (aug . 31) (sep . 30) (oct . 31) (nov . 30) (dec . 31)))
(define (dayscount taarich)
  (if (and (list? taarich)(= (length taarich) 3)(number? (car taarich))(integer? (car taarich))(assoc(cadr taarich) months)(number? (caddr taarich))(integer? (car taarich)))
      (SumOfDay taarich months)
      'error))
 (define(SumOfDay taarich listMonths [sum 0])
   (cond((equal? (cadr taarich) (caar listMonths))(+ sum (car taarich)))
        ((equal? (caar listMonths) 'feb)
        (if (YearKind taarich)
            (SumOfDay taarich (cdr listMonths) (+ sum 29))
            (SumOfDay taarich (cdr listMonths) (+ sum 28))))
        ((SumOfDay taarich (cdr listMonths) (+ sum (cdar listMonths))))))

(define(YearKind taar)
  (or (and (= (modulo (caddr taar) 4) 0) (not (= (modulo (caddr taar) 100) 0)))((= (modulo (caddr taar) 400) 0))))
;------3.2-------
(define(checknum N)
  (if (and (integer? N)(> N 0))
      (cond((=( modulo N (* 3 5 7))0) '(Bim Bam Bum))
           ((and(=( modulo N 3)0)(not(=(modulo N (* 5 7))0))) '( Bim))
           ((and(=( modulo N 5)0)(not(=(modulo N (* 3 7))0))) '( Bam))
           ((and(=( modulo N 7)0)(not(=(modulo N (* 3 5))0))) '( Bum))
           ((and(=( modulo N (* 3 5))0)(not(=(modulo N  7)0))) '(BimBam))
           ((and(=( modulo N (* 5 7))0)(not(=(modulo N  3)0))) '(BamBum))
           ((and(=( modulo N (* 3 7))0)(not(=(modulo N  5)0))) '(BimBum)))
      '(else)))
;answer
; (checknum 105)
;'(Bim Bam Bum)

;------3.3------
(define (middlenum L)
  (if(and(=(length L)3)(list? L)(not(or(equal? (car L) (cadr L))(equal? (car L) (caddr L)) (equal? (cadr L)(caddr L)))))
     (let* ((x (car L)) (y (cadr L)) (z (caddr L)))
     (if(not(or(eq? x (max x y z)) (eq? x (min x y z)))) x
        (if(not(or(eq? y (max x y z)) (eq? y (min x y z)))) x z))) '(ERROR_BY_'Middlenum'_FUNC)))
;answer
;(middlenum '(34 120 25))
;34
;(middlenum '(34 34 25))
;'(ERROR_BY_ 'Middlenum '_FUNC)

;------3.4-------
(define (num2digit N) (if(and(integer? N)(> N 0)(= 3(let* ((tmp N)(tmp1(string->list(number->string tmp)))) (length tmp1))))
                         (let*((N(number->string N))(N1(substring N 0 1))(N2(substring N 1 2)) (N3(substring N 2 3)))
                           (middlenum(append(list(string->number N1)(string->number N2)(string->number N3))))) '(ERROR_BY_'num2digit'_FUNC)))
;answer
;(num2digit 928)
;8
                          
                         
           
           
  

Y.levental

כתיבת תגובה

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