## Scheme Language – basic programs set 1

Here I have listed some basic programs in scheme language. Ask me if you have any doubts :).

_____________________

;;Bubble sort

(define (bubble inputList)

(define (swap inputList index1 index2)

(let ((item1 (list-ref inputList index1)) (item2 (list-ref inputList index2)))

(set-car! (list-tail inputList index1) item2)

(set-car! (list-tail inputList index2) item1)

)

)

(let loop1((count1 0) (len1 (- (length inputList) 1)))

(if(< count1 len1)

(begin

(let loop2((count2 (+ count1 1)) (len2 (length inputList)))

(if(< count2 len2)

(begin

(if(> (list-ref inputList count1) (list-ref inputList count2))

(begin

(swap inputList count1 count2)

(loop2 (+ count2 1) len2)

)

(loop2 (+ count2 1) len2)

)

) (loop1 (+ count1 1) len1)

)

)

)inputList

)

))

sample input:

(bubble (list 5 2 6 1 3))

output:

(1 2 3 5 6)

For calculating NCR

_______________

;;ncr calculation

(define (factorial num)

(if(= num 0)

1

(* num (factorial (- num 1)))

)

)

(define (ncr n r)

(if(inexact? n)

(let ((result 0))

(set! result (/ (factorial n) (* (factorial r) (factorial (- n r)))))

(display result))

))

input:

;(factorial 5.0)

(ncr 10.0 5)

output:

252.0

_______________________________________________

;;arithmetic progression

(define (arithprogress a d n)

(define result 0)

(let loop((temp 0) (start 0) (count n))

(if(< start count)

(begin

(set! temp (+ a (* start d)))

(set! result (+ result temp))

(loop result (+ start 1) count)

)

result

)

)

)

input:

(arithprogress 4 5 6)

output:

99