Learning basic programming concepts through Scheme with Simply Scheme, then with SICP.
(require (planet dyoo/simply-scheme:1:2/simply-scheme))
word
every ; map
keep ; filter
accumulate ; reduce
repeated
; condition
cond
if
'() ; = empty value
first ; = car
butfirst (remove first character/item) (bf) ; = cdr
butlast (bl)
append
prepend-every
item ; (item 4 '(return the fourth item in this sentence))
; to check type
number?
boolean?
word?
sentence?
; other predicates
even?
odd?
equal?
member?
before?
empty?Basic list manipuation function of scheme:
car ; = first
cdr ; = butfirst
cons ; = construct a new pair / list by prepending an element to the beginning of an existing list
;; (cons '1 '2)
;; (cons '0 '(1 2 3 4))- Define the global variables
(define pi 3.14)
pi
;; 3.14- Define a procedure
(define (sqrt x) (* x x))
(sqrt 4)
;; 16- Define anonymous function then invoke it
((lambda (x) (* x x)) 4)
;; 16- Define function that returns another function
(define (compose F G)
(lambda (x) (F (G x)))
)
((compose sqrt abs) -25)Define the local variables.
(let ((variable1 value1)
(variable2 value2)
...)
body-expressions)Func: if, cond
(define (switch-case-example value)
(cond
((= value 1) (display "Case 1"))
((= value 2) (display "Case 2"))
((= value 3) (display "Case 3"))
(else (display "Default case"))
)
)
; Example usage
(switch-case-example 2) ; This will display "Case 2"
(if (number? 'hihi) #t #f)
; #fevery, keep, accumulate, repeated