Skip to content

linhbuimai/sicp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 

Repository files navigation

What

Learning basic programming concepts through Scheme with Simply Scheme, then with SICP.

Simply Scheme

(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))

Special form syntax

define and lambda

  1. Define the global variables
(define pi 3.14)

pi
;; 3.14
  1. Define a procedure
(define (sqrt x) (* x x))

(sqrt 4)
;; 16
  1. Define anonymous function then invoke it
((lambda (x) (* x x)) 4)
;; 16
  1. Define function that returns another function
(define (compose F G)
  (lambda (x) (F (G x)))
)

((compose sqrt abs) -25)

let

Define the local variables.

(let ((variable1 value1)
      (variable2 value2)
      ...)
  body-expressions)

Predicates

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)
; #f

Higher order function

every, keep, accumulate, repeated

Resources

  1. Book: Simply Scheme (online reading)

  2. Link to download source code of Simply Scheme book

  3. Differences between Scheme and Common Lisp

About

SICP and 30s something

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published