5 Mart 2016 Cumartesi

Scheme Calısmalarım-Bazı kodlar



  • factoriel with lambda
                  (define fact 

                        (lambda (a)

                                 (if (= a 1) 1

                                        (* a (fact (- a 1))))))

          >(fact 5)
           120
  • fibonacci recursion ile 
(define (fib n)
  (cond ((= n 0) 0)
        ((= n 1) 1)
        (else (+ (fib (- n 1)) (fib (- n 2))))))


  • fibonacci iteration ile
(define (fibo x)
  (fib-iter 1 0 x))
(define (fib-iter a b x)
  (cond ((= x 0) b)
        (else (fib-iter (+ a b) a (- x 1)))))


  •  Exponation recursion
(define (uzeri x y)
  (cond ((= y 0) 1)
        (else (* x (uzeri x (- y 1))))))



  • Exponantion Iteration

(define (ussu a b)
  (ussu-iter 1 a b))
(define (ussu-iter x a b)
  (cond ((= b 0) x)
        (else (ussu-iter (* x a) a (- b 1)))))


  •  Farklı bir sekilde liste uzunlugu bulma
> (define list-length (lambda (list) (if (null? list) 0 (+ 1 (list-length (cdr list))))))
> (define lst '(1 2 3 4))
> (list-length lst)
4

  •  Sum of list elements

    (define sum-list (lambda (list) (if (null? list) 0 (+ (car list) (sum-list (cdr list))))))
    > (sum-list lst)
    10

  • Product of list elements


> (define prod-list (lambda (list) (if (null? list) 1 (* (car list) (prod-list (cdr list))))))
> (prod-list lst)
24

  •  Bir listedeki en buyuk elemanı bulma

(define lst '(1 5 3 7 4))
(define (getlargest a_list)
  (cond
    ((null? a_list)      
     #f)
    ((null? (cdr a_list))
     (car a_list))
    ((< (car a_list) (cadr a_list))
     (getlargest (cdr a_list)))
    (else
     (getlargest (cons (car a_list) (cddr a_list))))))
  • En buyuk elemanı bulma 2.YOL
(define (en-buyuk-eleman list) (cond ((empty? list) (error "empty list"))
                                     ((= (length list) 1) (car list))
                                     (else (max (car list) (en-buyuk-eleman (cdr list))))))

  • Determining whether a list contains that item or not
(define (find item list)(cond ((empty? list) false)
                              ((= item (car list)) true)
                              (else (find item (cdr list)))))

  • Listeye eleman ekleme

(define (eleman-ekle eleman liste)
  (cond ((or (empty? liste) (<= eleman (car liste))) (cons eleman liste))
        (else (cons (car liste) (eleman-ekle eleman (cdr liste))))))

  •   Listedeki elemanları sıralama

(define (eleman-ekle eleman liste)
  (cond ((or (empty? liste) (<= eleman (car liste))) (cons eleman liste))
        (else (cons (car liste) (eleman-ekle eleman (cdr liste))))))
(define (sort-liste liste)
  (cond ((empty? liste) empty)
        (else (eleman-ekle (car liste) (sort-liste (cdr liste))))))

  •  Listeyi tersten yazdırma

(define (ters l) (if (empty? l) empty
                           (append (ters (cdr l)) (list (car l)))))
İlişkisel Operatörler (Relational Operators)
İlişkisel Operatörler Scheme dilinde mevcuttur:
(= x 1)      x, 1 ise true döner
(< x y)      x, y dan küçük ise true döner
(<= x y)      x, y dan küçük veya eşitse true döner
(> x y)      x, y dan büyük ise true döner
(>= x y)      x, y dan büyük veya eşit ise true döner
(eq? x y)      x ve y ayni ise true döner
(eqv? x y)      x ve y işlevsel olarak ayni ise true döner
(equal? x y)      x veya aynı yapıda olup ayni içeriğe sahip ise true döner
Type Predicates (Predicate sonucu true ya da false donen fonksiyonlardir.)
Scheme programlama dilinde Tip Yüklemleri şunlardır:
(procedure? x)       x fonksiyon ise true döndürür
(null? x)           x boş bir liste ise true döndürür
(zero? x)           x sıfır ise true döndürür
(odd? x)           x tek ise true döndürür
(even? x)           x çift ise true döndürür
(boolean? x)        x Boolean ise true döndürür
(number? x)          x sayi ise true döndürür
(pair? x)          x bir çift ise true döndürür
(symbol? x)          x bir sembol ise true döndürür
LET KOMUTU
 Let komutuyla programın belirli bir alanına değişkenlere isteğiniz üzere değer ataması yapabilirsiniz. Belirlenen değerler o kod blok’u içinde geçerlidir. Kod blogundan çıkınca, değişkenlere atanmış değerler geçerli olmayacaktır.

(define x 45)
(let ((x 36)) (sqrt x))
 (sqrt x)
6
Goruldugu gibi x e atanan son degere gore program calısır.

Diger bir ornek:

> (define x 15)
>(let ((x 1) (y x) (+ x y))
>16



Burada x'e 1 degeri atanmıs y'ye ise x'in degeri olan 15 degeri (global degeri) atanmıstır.Sondaki toplamanın sonucu 16 cıkacaktır.




Soru1:
(define x 10)
(let ((x (* x x)) (y (+ x 10)))
(+ x y 10))
Cevabını bulunuz. (En altta)

Matematik Fonksiyonlar
(exp x) e^x in sonucunu döndürür
(log x) x sayısının logaritma değerini döndürür
(sqrt x) x kökünün karesini döndürür
(max x1 x2…) Verilen listeden en büyük sayıyı döndürür
(min x1 x2…) Verilen listeden en küçük sayıyı döndürür
(quotient x1 x2) x1/x2 kesrinin bölümünün sonucunu döndürür
(remainder x1 x2) x1/x2 kesrinin bölümünün sonucunu döndürür
(modulo x1 x2) x1 in x2 ye modüle sonucunu dündürür
(gcd num1 num2 …) Verilen listenin en büyük ortak böleni döndürür
(lcm num1 num2 …) Verilen listenin en küçük ortak çarpanını döndürür
(expt base power) taban^küvvet sonucunu döndürür
(sin x),(cos x),(tan x),(asin x),(acos x),(atan x) trigonometrik fonksiyonlar
  • TOPLAMA

(define (list-toplami list) (cond ((empty? list) 0)

                                  (else (+ (list-toplami (cdr list)) (car list)))))

  • LAMBDA ILE TOPLAMA 
(define toplam (lambda (list) (if (empty? list) 0 (+ (toplam (cdr list)) (car list)))))




An Example Program

The purpose of the following function is to help balance a checkbook. The function prompts the user for an initial balance. Then it enters the loop in which it requests a number from the user, subtracts it from the current balance, and keeps track of the new balance. Deposits are entered by inputting a negative number. Entering zero (0) causes the procedure to terminate and print the final balance.
(define checkbook (lambda ()

; This check book balancing program was written to illustrate
; i/o in Scheme. It uses the purely functional part of Scheme.

        ; These definitions are local to checkbook
        (letrec

            ; These strings are used as prompts

           ((IB "Enter initial balance: ")
            (AT "Enter transaction (- for withdrawal): ")
            (FB "Your final balance is: ")

            ; This function displays a prompt then returns
            ; a value read.

            (prompt-read (lambda (Prompt)

                  (display Prompt)
                  (read)))

            ; This function recursively computes the new
            ; balance given an initial balance init and
            ; a new value t.  Termination occurs when the
            ; new value is 0.

            (newbal (lambda (Init t)
                  (if (= t 0)
                      (list FB Init)
                      (transaction (+ Init t)))))

            ; This function prompts for and reads the next
            ; transaction and passes the information to newbal

            (transaction (lambda (Init)
                      (newbal Init (prompt-read AT)))))

; This is the body of checkbook;  it prompts for the
; starting balance

  (transaction (prompt-read IB)))))







http://www.scheme.com/tspl3/examples.html


cevap1 :130








Finding the given number is perfect or not.

For a number to be perfect ; sum of divisors of that number that are smaller than the number must equal to that number.For example lets look for 6. 1 + 2 + 3 = 6. So 6 is perfect number. 28 and 496 and 8128 are also perfect number.


(define sumBolum
  (lambda (x y z)
    (if (< x y)
        (if (equal? 0 (remainder y x)) (sumBolum (+ x 1) y (+ z x)) (sumBolum (+ x 1) y z))
        z)))
(define perfectNumber
  (lambda (a)
    (if (equal? a (sumBolum 1 a 0)) #t #f)))

>(perfectNumber 6)
#t
>(perfectNumber 28)
#t
>(perfectNumber 8)
#f



1 yorum: