Skip to content

Instantly share code, notes, and snippets.

View Liutos's full-sized avatar

LiuTao Liutos

View GitHub Profile
(defun loop-random (times)
(with-output-to-string (*standard-output*)
(loop
:for i :from 1 :to times
:do (format t "~A" (random 10)))))
@Liutos
Liutos / gist:3597752
Created September 2, 2012 12:03
举个有惰性可以方便的例子
private void func(int data) {
long time = getTime(); // 如果没有惰性,那么这里一定会被求值。
switch(data) {
case 1:
funcNeedTime1(data, time); // 需要使用time变量
break;
case 2:
funcNeedTime2(data, time); // 需要使用time变量
break;
@Liutos
Liutos / eprogn.scm
Created September 4, 2012 07:59
《Lisp in Small Piece》第9页小小读书笔记
;;; 这是《Lisp in Small Piece》上第9页的eprogn函数的定义,实际上这样子的定义
;;; 是为了可以让调用eprogn的返回值可以是最后一个被求值的表达式。
(define (eprogn exps env)
(if (pair? exps)
(if (pair? (cdr exps))
(begin (evaluate (car exps) env)
(eprogn (cdr exps) env))
(evaluate (car exps) env))
'()))
@Liutos
Liutos / gist:3631848
Created September 5, 2012 06:39
传递一个不需要的参数的递归函数,纯粹测试。
;;; 下面的函数定义中参数c没有使用到,在我的系统上使用SBCL,在TopLevel中输入下面的表达式会抛出警告,说变量c没有使用。
(defun my-plus (a b c)
(if (zerop b)
a
(my-plus (1+ a) (1- b) nil)))
@Liutos
Liutos / gist:3726523
Created September 15, 2012 06:01
照着垠神的λ-calculus解释器用CommonLisp写的
(defpackage :com.liutos.del0-interp
(:use :cl)
(:nicknames :del0-interp
:interp))
(in-package :interp)
(defvar *env0* '())
(defun ext-env (x v env)
@Liutos
Liutos / max-incseq.lisp
Created September 16, 2012 10:09
求``最长递增子序列''
(defpackage :com.liutos.max-incseq
(:use :cl)
(:nicknames :max-incseq)
(:documentation "计算一个序列的最长递增子序列"))
(in-package :max-incseq)
(defun max-incseq/aux (vec)
"返回中间结果的数组,存储了到达各个数的最长递增子序列的长度。"
(declare (type vector vec))
@Liutos
Liutos / editance.lisp
Created September 19, 2012 08:40
计算两个字符串之间的编辑距离,并以文字形式进行转换过程的报告。
(defpackage :com.liutos.editance
(:use :cl))
(in-package :com.liutos.editance)
(defmacro for ((var start end) &body body)
`(do ((,var ,start (1+ ,var)))
((> ,var ,end))
,@body))
@Liutos
Liutos / maxSubseq.ml
Created September 23, 2012 15:43
在给定的数组中找出各元素之和在所有子集中最大的子数组
let maxSubseq seq =
let aux = Array.create (Array.length seq) 0
and len = Array.length seq
and iStart = ref 0
and iEnd = ref 0
and max = ref 0
in
begin
aux.(0) <- seq.(0);
max := seq.(0);
@Liutos
Liutos / gist:3792922
Last active February 27, 2018 20:49
我所捏造出来的语言的样子\( ̄▽ ̄)/
;;;; Code Examples but the language wasn't implemented yet.
;;; 定义函数factorial
defun factorial (n) ;; 注释以两个分号开始,至一行的末尾结束。
if 0 = n ;; 表示两个对象之间的关系的函数用中缀形式表示
1
n * factorial(n - 1) ;; if的alternate部分的表达式比起consequence的部分向左移动一个空格,但是其实并非像Python那样用缩进决定语法结构。
;;; 尾递归版本的factorial,使用了内部的辅助函数。
;;; 可以这样表达的内部函数需要语言支持词法作用域,并且defun也应该是这样的,而不是像CL那样定义全局函数。
/*
* main.c
*
*
*
* Copyright (C) 2012-10-01 liutos
*/
#include "type.h"
#include <stdio.h>