Skip to content

Instantly share code, notes, and snippets.

@txominpelu
Created March 14, 2014 06:30
Show Gist options
  • Save txominpelu/9543009 to your computer and use it in GitHub Desktop.
Save txominpelu/9543009 to your computer and use it in GitHub Desktop.
#lang scheme
(define (convert char)
(cond
[(char=? char #\<)
"&lt;"]
[(char=? char #\>)
"&gt;"]
[else (string char)]))
(define (escape mystring)
(cond
[(empty? mystring) '()]
[else
(let ([converted (convert (car mystring))])
(cons converted (escape (cdr mystring))))]))
(escape (string->list "<abcde>"))
(define (mlist x y . l)
l)
(mlist 1 2 3 4)
(define (mvector x . l)
(letrec ([vec (make-vector (+ 1 (length l)))]
[step
(lambda (elem left index)
(cond
[(empty? left)
(begin
(vector-set! vec index elem)
vec)]
[else (begin
(vector-set! vec index elem)
(step (car left) (cdr left) (+ 1 index)))]))])
(step x l 0)))
(mvector 1 2 3 4)
(define (mreverse l)
(letrec ([step (lambda (left acc)
(cond
[(empty? left) acc]
[else (step (cdr left) (cons (car left) acc))]))])
(step l '())))
(mreverse '(1 2 3 4))
((lambda (x) (+ ((lambda (x) (* x x)) x) ((lambda (x) (* x x)) x))) 2)
(define (map2 f l)
(letrec
([step (lambda (left)
(cond
[(empty? left) '()]
[else (cons (f (car left)) (step (cdr left)))]))])
(step l)))
(map2 (lambda (x) (+ x 1)) (list 1 2 3 4))
(map2 car vectors)
(define (map f . vectors)
(cond
[(empty? vectors) (error 'failed)]
[(empty? (car vectors)) '()]
[else
(letrec
([first (map2 car vectors)]
[left (map2 cdr vectors)])
(cons (apply f first) (apply map (cons f left))))]))
(map + '(1 2 3) '(4 5 6))

let

(let ([var1 value1] [var2 value2]) expr)

** Note: **

In:

(let ([var1 value1] [var2 value2]) expr)

I cannot use the value of var1 to calculate value2.

let* allows to do that so for example the following is allowed:

(let* ([x 1] [y (+ x 1)]) (list y x))

letrec allows to use a var to calculate the value of a value recursively. So the following is allowed:

(let* ([x 1] [y (+ x 1)]) (list y x))

function arity

(define (first x . l) l) (define (rest x . l) l)

(first 1 2 3 4) ;; 1 (rest 1 2 3 4) ;; (2, 3, 4)

Conditionals

(cond [(cond1) result1] ... [(condn) resultn] [else elseresult])

Match-case

(match-case x ((unless ?test . ?rest) `(if (not ,test) ,@rest)) (else x))

  • ?arg assigns a value to arg

Conversion functions

(string->list str) (list->string lst) ....

Macros

(define-macro (macro-name params)

after ` everything is interpreted normally but: ,varname // splices the variable ,@params // splices a list into the elements

(symbol->keyword id)

Case

(case m ((car) a) ((cdr) d) ((set-car!) (set! a v)) ((set-cdr!) (set! d v)))))

String

(format "tal/~a" "cual") // = tal/cual

Characters

Are literals quoted with #, eg:

#\A

HTML

( :css "../styles.css") ()

Javascript

( :onmouseover ~(innerHTML-set! this "h"))

  • To access a server variable use $:

( :onmouseover ~(innerHTML-set! this $m))

  • Call a service
(module mylib
(export (get-input-value id)))
(define count 0)
(define lock (make-mutex))
(define-service (inc)
(synchronize lock
(set! count (+ 1 count))
(<HTML> count)))
(define-service (showarg #!key arg1)
(<HTML>
(<HEAD> :css "/Users/zenexity/Documents/master-2013/web-diffuse/ex-s03.css")
(<BODY>
(<DIV>
(map (lambda (m) (<SPAN>
:onmouseover ~(innerHTML-set! this
(with-hop $(inc) (lambda (c)
(begin
(display c)
(char-upcase $m)))))
:onmouseleave ~(innerHTML-set! this $m) m))
(string->list arg1))))
)
)
(define (get-input-value id)
(js-property (dom-get-element-by-id id) "value") )
(define-service (guess #!key guess)
(let ((number 8))
(<HTML>
(<BODY>
(<DIV>
(<INPUT> :id "input")
(<BUTTON> :onclick ~(if (eq? $number (string->number (get-input-value "value")) ) (display #t) (display #f)) "Try"))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment