Skip to content

Instantly share code, notes, and snippets.

@zestime
zestime / lookahead.py
Created March 13, 2015 10:24
lookahead & lookbehind in python
>>> import re
>>> msg1 = "aaabbbccc"
>>> msg2 = "dddbbbeee"
>>> re.findall("bbb(?=ccc)", msg1) # lookahead
['bbb']
>>> re.findall("bbb(?=ccc)", msg2)
[]
>>> re.findall("bbb(?!ccc)", msg1) # negative lookahead
[]
>>> re.findall("bbb(?!ccc)", msg2)
(define (make-account balance key)
(let ((try 0))
(define (withdraw amount)
(if (>= balance amount)
(begin (set! balance (- balance amount))
balance)
"Insufficient funds"))
(define (deposit amount)
(set! balance (+ balance amount))
balance)
(define (make-account balance key)
(define (withdraw amount)
(if (>= balance amount)
(begin (set! balance (- balance amount))
balance)
"Insufficient funds"))
(define (deposit amount)
(set! balance (+ balance amount))
balance)
(define (dispatch k m)
(define (make-accumulator init)
(lambda (value)
(begin (set! init (+ init value))
init)))
; No change, as same as before
(define (element-of-set? x set)
(cond ((null? set) false)
((equal? x (car set)) true)
(else (element-of-set? x (cdr set)))))
; Just use cons, don't need to use element-of-set anymore
(define (adjoin-set x set)
(cons x set))
(define (element-of-set? x set)
(cond ((null? set) false)
((equal? x (car set)) true)
(else (element-of-set? x (cdr set)))))
(define (adjoin-set x set)
(if (element-of-set? x set)
set
(cons x set)))
(define (myequal? col1 col2)
(if (and (pair? col1) (pair? col2))
(and (eq? (car col1) (car col2)) (myequal? (cdr col1) (cdr col2)))
(eq? col1 col2)))
(myequal? '(this is a list) '(this is a list)) ; #t
(myequal? '(this is a list) '(this (is a) list)) ; #f
@zestime
zestime / sicp-2.53.lisp
Last active August 29, 2015 14:11
SICP Solution
(list 'a 'b 'c) ; '(a b c)
(list (list 'george)) ; '((george))
(cdr '((x1 x2) (y1 y2))) ; '((y1 y2))
(cadr '((x1 x2) (y1 y2))) ; '(y1 y2)
(pair? (car '(a short list))) ; #f
(memq 'red '((red shoes) (blue socks))) ; #f
(memq 'red '(red shoes blue socks)) ; '(red shoes blue socks)