Skip to content

Instantly share code, notes, and snippets.

@aoeuidht
aoeuidht / gist:3728553
Created September 15, 2012 15:52
SICP Exercise 1.3
;Exercise 1.3. Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers.
(define (sum_squares_top2 c1 c2 c3)
(+
(square max(c1 c2))
(square max((min c1 c2) c3))))
@aoeuidht
aoeuidht / gist:3946032
Created October 24, 2012 13:24
how Opera xhrproxy handle post params
if request.method == 'POST':
fields = request.GET['_proxy_fields'].split(',')
params = {}
for field in fields:
if len(field) == 0: continue
params[field] = request.POST[params]
if request.method == 'POST':
data = urllib.urlencode(params)
else:
@aoeuidht
aoeuidht / gist:3973003
Created October 29, 2012 11:14
1.11 of sicp
;A function f is defined by the rule that f(n) = n if n<3 and
; f(n) = f(n - 1) + 2f(n - 2) + 3f(n - 3) if n> 3.
; Write a procedure that computes f by means of a recursive process.
; Write a procedure that computes f by means of an iterative process.
(defun func_f (n)
(if (< n 4) n
(+ (func_f (- n 1))
(* 2 (func_f (- n 2)))
(* 3 (func_f (- n 3))))))
@aoeuidht
aoeuidht / gist:3978427
Created October 30, 2012 05:13
SICP Exercise 1.12 -- Pascal's triangle
;The following pattern of numbers is called Pascal's triangle
(defun pascal_x_y (x y)
(if (or (= y 0) (= x y))
1
(+ (pascal_x_y (- x 1) (- y 1))
(pascal_x_y (- x 1) y))))
(defun draw_line (n)
(loop for idx from 0 to n do
(prin1 (pascal_x_y n idx))
@aoeuidht
aoeuidht / gist:3978843
Created October 30, 2012 07:45
SICP Exercise 1.17 -- fast-times
; Now suppose we include, together with addition,
; operations double, which doubles an integer,
; and halve, which divides an (even) integer by 2.
; Using these, design a multiplication procedure analogous to
; fast-expt that uses a logarithmic number of steps.
(defun fast_times (multiplicand multiplier)
(cond ((= multiplier 0) 0)
((even? multiplier)
(double (fast_times multiplicand (half multiplier))))
(else
@aoeuidht
aoeuidht / gist:4014719
Created November 5, 2012 01:25
xhrproxy still not working with 「content-type」 in _proxy_headers
curl -vvv -H "Content-Type: xml" -H "charset: utf8" -d"a=b" "http://netease-news.tv.opera.com/xhrproxy/?_proxy_fields=&_proxy_headers=content-type,charset&_proxy_url=http%3A//echo.opera.com"
@aoeuidht
aoeuidht / gist:4143224
Created November 25, 2012 11:53
xhrproxy should support gbk encoding
def decode(s, encodings=('gbk', 'utf8', 'windows-1251', 'iso-8859-1')):
for encoding in encodings:
try:
return s.decode(encoding)
except UnicodeDecodeError:
pass
return s.decode('iso-8859-1', 'ignore')
def sigma(n):
return n and (n + sigma(n-1)) or 0
def sigma_tail(n, sum=0):
return n and sigma_tail((n-1), (n+sum)) or sum
print sigma(50)
print sigma_tail(50)
@aoeuidht
aoeuidht / gist:5402272
Created April 17, 2013 06:50
generate json files in my documentation
#! /usr/bin/sbcl --script
(load "~/quicklisp/setup.lisp")
(with-open-file (*standard-output* "/dev/null" :direction :output
:if-exists :supersede)
(ql:quickload "cl-json"))
(defmacro rep-item (item rep-cnt)
`(loop for i from 1 to ,rep-cnt collect ,item))
(let ((gossip-item '((a . liszt)(c . msg1)(ts . 123456)))
(part-item '((nick . liszt)(uid . a1b2c3d4)(loc . "(1234,5678)")
@aoeuidht
aoeuidht / exericse-1.11.lisp
Last active December 17, 2015 20:49
exericse 1.11
(defun calc-f (n)
(if (< n 3)
n
(+ (calc-f (- n 1))
(* (calc-f (- n 2)) 2)
(* (calc-f (- n 3)) 3)
))
)
(defun calc-f (n)