## Scheme Language – basic programs set 2

; string reverse

(define (stringrev inputString)

(define len (string-length inputString))

(define result “”)

(let loop((count len))

(if(> count 0)

(begin

(set! result (string-append result (make-string 1 (string-ref inputString (- count 1)))))

(loop (- count 1))

))

result

)

)

input:

;(stringrev “karthi”)

output:

“ihtrak”

_____________________________________

; fibonacci series

(define (fibonacci num)

(define num1 0)

(define num2 1)

(define result(list))

(let loop((count 0) (temp 0))

(if(< count num)

(begin

(set! result (append result (list num1)))

(set! temp num2)

(set! num2 (+ num1 num2))

(set! num1 temp)

(loop (+ count 1) temp)

)

result

)

)

)

input:

(fibonacci 5)

output:

(0 1 1 2 3)

___________________________

;struct

(define-struct std(roll name))

(define (pro clist)

(std-roll (list-ref clist 0))

(set-std-roll! (list-ref clist 0) 2)

(std-roll (list-ref clist 0))

)

(pro (list (make-std 11 “gg”) (make-std 12 “tt”)))

2

______________________________

(define (checkprime num)

(if(< num 2)

#f

(begin

(let loop((count 2) (end (round(sqrt num))))

(if(<= count end)

(begin

(if(= (modulo num count) 0)

#f

(loop (+ count 1) end)

)

)

#t

)

)

)

)

)

;(checkprime 8)

;(checkprime 9)

;(checkprime 1)

;(checkprime 2)

(define (prime num)

(define result(list))

(let looping((count 2) (end num))

(if(<= count num)

(begin

(if(checkprime count)

(begin

(set! result (append result (list count)))

(looping (+ count 1) end)

)

(looping (+ count 1) end)

)

)

result

)

)

)

input:

(prime 50)

output:

(2 3 5 7 11 13 17 19 23 29 31 37 41 43 47)