3.λ.形而下技术博客

这里只关注技术实现,用代码说话。

0%

宏是怎么扩展的呢?

比较流行的编程语言中,Java,Python都没有定义宏的功能。c语言中的宏限制比较大, 没有Lisp中的宏强大。以下提到的宏,指的是Lisp方言中的宏。

宏的求值可以分为两个步骤

  1. 扩展(expand)–编译期
  2. 求值(eval) –运行期

这看起来很简单,但是实际写macro的时候,就会有很多的困惑。这里的主要矛盾就是理解 以下两个过程,

  1. 在一个宏的内部调用另一个函数
  2. 在一个宏的内部调用另一个宏

不同的lisp方言有不同的实现。以下用Clojure来研究宏的用法。

阅读全文 »

题目概述

原题链接

Given a data stream input of non-negative integers a1, a2, …, an, …, summarize the numbers seen so far as a list of disjoint intervals.

For example, suppose the integers from the data stream are 1, 3, 7, 2, 6, …, then the summary will be:

[1, 1] [1, 1], [3, 3] [1, 1], [3, 3], [7, 7] [1, 3], [7, 7] [1, 3], [6, 7]

阅读全文 »

题目概述

原题链接

Given a nested list of integers, implement an iterator to flatten it.

Each element is either an integer, or a list – whose elements may also be integers or other lists. Input: [[1,1],2,[1,1]] Output: [1,1,2,1,1]

阅读全文 »

题目概述

原题链接

The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that "all houses in this place forms a binary tree". It will automatically contact the police if two directly-linked houses were broken into on the same night.
Determine the maximum amount of money the thief can rob tonight without alerting the police.
1
2
3
4
5
6
7
Example 1:
3
/ \
2 2
\ \
4 1
Maximum amount of money the thief can rob = 3 + 4 + 1 = 8.
阅读全文 »

问题

这是一个经典题,面试的时候经常会遇到。 例如:文件的内容如下

26329
46184
94842
9036
96555
40954
38187
15548
51452
861
51010
8721
13666
69837 

每行一个数字,找到出现次数最多的数。通常的解法是遍历文件一遍,用hashmap维护一个(number => freq), 然后用最小堆找到最大的10个数。

阅读全文 »

题目概述

312原题链接

Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by array nums. You are asked to burst all the balloons. If the you burst balloon i you will get nums[left] * nums[i] * nums[right] coins. Here left and right are adjacent indices of i. After the burst, the left and right then becomes adjacent.

Find the maximum coins you can collect by bursting the balloons wisely.

examples:
nums = [3,1,5,8] --> [3,5,8] -->   [3,8]   -->  [8]  --> []
coins =  3*1*5      +  3*5*8    +  1*3*8      + 1*8*1   = 167
阅读全文 »

问题

Java世界里构建项目用什么工具呢,ant,maven和gradle。maven非常流行, 原因无非是maven仓库和项目架构的约定。但是ant构建过程更加容易理解, 因为ant展示了所有的操作。gradle拥有基于groovy语言的DSL语言且继承了 maven仓库的思想所以笔者认为未来是属于gradle的。

阅读全文 »

MiniLisp是什么?

MiniLisp是一个只用几百行代码实现的Lisp。以下是实现的 基本特性。

  • integers, symbols, cons cells,
  • global variables,
  • lexically-scoped local variables,
  • closures,
  • if conditional,
  • primitive functions, such as +, =, <, or list,
  • user-defined functions,
  • a macro system,
  • and a copying garbage collector.
阅读全文 »