scheme תרגיל 4

bennylisp, Racket, schemeLeave a Comment

scheme תרגיל 4 תרגיל בשפת LISP, מציאת איברים ראשוניים ועוד פעולות רקורסיביות ..

&nbsp


#lang racket


(define(inputCorrect2? NUM)
  (if (and (> NUM 0) (= (modulo NUM 1) 0  )) #t #f))


(define(prime?  N)
  (if (= N 1) #t
     (every-something (rangelst N) N))   ;(rangelst N) is  a list (2,3,4...N/2)
  )

; this func send recive N and send N/2 to the createList func (2 parameters: 2,n/2)
(define (rangelst N)
  (createList 2 (truncate(/ N  2)))
  )
 ;createList func return a list (the elements are: 2,3,4,..... END ( END is N/2) )
(define (createList START END)
  (if(= START (+ END 1))
     null
     (cons START (createList ( + START 1 ) END))
     ))
 
(define (divisible N M)
  (if(=(modulo N M) 0)   #t    #f)
 
)

; the recurciv function in evry pattern 
 (define (every-something L N)
    (if (null? L ) #t
        (if (divisible N (car L)) #f 
            (every-something (cdr L) N  )
            )
        )
 
 )

; question 2   
(define (primes N)
  
(primesList(createList 1 N))

  )

(define (primesList L)
    (if (null? L )null
        (if (prime? (car L)) (cons (car L) (primesList (cdr L)))
            (primesList (cdr L))))
 
 )
; question 4 
(define(ziplst L1 L2)
  (if (or(null? L1) (null? L2)) null
      (cons (cons (car L1) (cons (car L2) null))  (ziplst (cdr L1) (cdr L2)) )
      )
  )

; question 3    // output 
;> (erathosprimes1 10)
;'(1 2 3 5 7)

(define(erathosprimes1 N)
  (if (inputCorrect2? N)
  (erathosprimes1Filter (erathosprimes1Help2 (createList 1 N) 2) )
  "error input"
  ))                                                                


(define(erathosprimes1Help L I K)
  (if (null? L) null
      (if (and(equal? (car L) #f)(= K I)) L
            (if (equal? K (+ I 1)) 
              (if (equal? (car L) #f) (cons (car L) (erathosprimes1Help (cdr L) I K))
                 (if (=(modulo (car L) I)0) (cons #f (erathosprimes1Help (cdr L) I K) ) 
         (cons (car L) (erathosprimes1Help (cdr L) I K)) )
     )
   (cons (car L) (erathosprimes1Help (cdr L) I (+ K 1))))
      )))


(define (erathosprimes1Help2 L I)
  (if (= (length L) (+ I 1)) L
        (erathosprimes1Help2 (erathosprimes1Help L I 1) (+ I 1))  )
      )


(define(erathosprimes1Filter L)
  (if (null? L) null
      (if (equal? (car L) #f) (erathosprimes1Filter (cdr L))
                  (cons (car L) (erathosprimes1Filter (cdr L))
                        ))))
      


(define(erathosprimes2Help L I K [RESULT null])
  (if (null? L) RESULT
      (if (and(equal? (car L) #f)(= K I)) RESULT
            (if (equal? K (+ I 1)) 
              (if (equal? (car L) #f) ( RESULT  (cons (car L) (erathosprimes1Help (cdr L) I K)RESULT))
                 (if (=(modulo (car L) I)0) (cons #f (erathosprimes1Help (cdr L) I K) ) 
         (cons (car L) (erathosprimes1Help (cdr L) I K)) )
     )
   (cons (car L) (erathosprimes1Help (cdr L) I (+ K 1))))
      )))


פתרון התרגיל לא הועלה בשלמותו

כתיבת תגובה

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