Created
November 23, 2013 11:19
-
-
Save vaishaks/7613414 to your computer and use it in GitHub Desktop.
Scheme basics.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(+ 3 (* 4 5)) ;A basic scheme expression | |
(define a 1) ;Defining a value | |
(define b 1) | |
(and (= a b) (not (= a 0))) ;A logical expression | |
(display "Enter a number: ") ;Output | |
(define x (read)) ;Input | |
(display "The square of that number is ") | |
(display (* x x)) (newline) ;\n | |
(quote (2.1 2.2 2.3)) ;Return something as is | |
'scheme ;Shorthand for quote | |
(if (= a 0) 0 ;If-Else | |
(/ 1 a)) | |
(cond ((= a 0) 0) ;If-Elseif | |
((= a 1) 1) | |
(else (/ 1 a))) | |
(let ((a 2) (b 3)) (+ a b)) ;binding list | |
(display "Area of a circle with radius ") | |
(display x) | |
(display " is: ") | |
(display ((lambda (radius) (* 3.14159 (* radius radius))) x)) ;Function definition | |
(newline) | |
(let ((circlearea (lambda (radius) (* 3.14159 (* radius radius))))) (circlearea x)) ;Using let to give the function a name | |
(display "The factorial of ") | |
(display x) | |
(display " is: ") | |
(display | |
(letrec ;letrec for naming recursive funtions | |
((factorial | |
(lambda(n) | |
(if (= n 0) 1 | |
(* n (factorial (- n 1))))))) | |
(factorial x))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment