Skip to content

Instantly share code, notes, and snippets.

View Liutos's full-sized avatar

LiuTao Liutos

View GitHub Profile
@Liutos
Liutos / postorder_output.c
Created October 25, 2012 10:13
根据先序和中序输出节点序列构造二叉树并输出其后序遍历的程序
/*
* postorder_output.c
*
* Generates a binary tree according to the two outputs of preorder and
* inorder traversal and outputs a sequence of nodes by postorder traversal.
*
* Copyright (C) 2012-10-25 liutos <[email protected]>
*/
#include <stdlib.h>
#include <string.h>
@Liutos
Liutos / TreePost.java
Created October 25, 2012 12:02
改进了上一个Gist中的make_tree_by_prein函数以避免过多中间字符串的产生
/*
* TreePost.java
*
*
*
* Copyright (C) 2012-10-25 liutos
*/
class TreeNode
{
char value;
@Liutos
Liutos / tree-post.lisp
Created October 25, 2012 14:25
根据先序和中序输出结构构造完整的CommonLisp代码
(defpackage :com.liutos.binary-tree
(:use :cl))
(in-package :com.liutos.binary-tree)
(defclass tree-node ()
((value :initarg :value
:accessor tree-node-value)
(left :initarg :left
:accessor tree-node-left)
@Liutos
Liutos / mvb.lisp
Created October 26, 2012 09:02
多重返回值的演示
(truncate 10 3) ;一般情况下返回值为3,即10除以3的商,余数被丢弃了。
(multiple-value-bind (q r)
(truncate 10 3)
(format t "~D and ~D" q r)) ;输出结果为"3 and 1"。truncate实际上是会返回
;多个值(即多重返回值)的一个函数,不过一般只会得
;到一个,所以要用宏multiple-value-bind来捕捉
;所有的返回值。相当于把(truncate 10 3)的第一个
;返回值赋值给了q,第二个赋值给了r。
@Liutos
Liutos / gist.ml
Created December 10, 2012 12:50
OCaml代码示例
(* 定义变量a的值为1 *)
let a = 1
(* 定义匿名函数,接收一个整数作为参数,返回结果为其参数的两倍 *)
function x -> x*2
(* 定义名为doubel的函数,功能和上面的匿名函数相同 *)
let double = function x -> x*2
(* 等价的写法,即语法糖 *)
let double x = x*2
@Liutos
Liutos / tokenizer.c
Created December 11, 2012 06:31
tokenizer函数演示
typedef enum {
LPARENTHESIS,
RPARENTHESIS,
SYMBOL_TOKEN,
EOF_TOKEN,
};
void consume(lexer_t lexer)
{
if (lexer->c != '\0') {

关于'抄袭'这个事情,事实上我们(lispers)有三个观点: 1,很多语言抄袭了最初来自LISP的特性; 2,没有一种语言在抄袭时抄到了该LISP特性的精髓; 3,即使没抄到精髓却也已经够用了; ——————田春冰河

首先要看着顺眼,再来考虑语言特性什么的。 ——————E.T

@Liutos
Liutos / parse-params.lisp
Created February 3, 2013 02:29
The code with optimization declaration may make SBCL to note some *unable to optimize* message.
(defun parse-params (s)
(labels ((aux (acc s)
(declare (optimize (speed 3)))
(let ((i1 (position #\= s))
(i2 (position #\& s)))
(cond (i1 (aux (cons (cons (intern (string-upcase (subseq s 0 i1)))
(decode-param (subseq s (1+ i1) i2)))
acc)
(and i2 (subseq s (1+ i2)))))
((equal s "") nil)
@Liutos
Liutos / from_utf8.c
Created October 19, 2013 14:31
在UTF-8和Code Point之间转换的代码及配套工具
#include <stdio.h>
#include <stdlib.h>
#define MASK 0x8000
// 计算一个字节中最高位开始的连续为1的位的数量
int count1(char byte) {
int count = 0;
while ((byte & MASK) == MASK) {
count++;
(set lst #(1 2 3))
(set vec [4 5 6])
(set tab {1 => 'a', 2 => 'b'})
(with lst.each do (x)
(Stream.println x))
(with vec.each do (x)
(Stream.println x))