Your time is limited, so don’t waste it living someone else’s life. Don’t be trapped by dogma — which is living with the results of other people’s thinking. Don’t let the noise of others’ opinio...
Category Basics
It seems inevitable that, after one learned Haskell, there’s a good possibility she will become a victim of category theory. This is a series(hopefully) of notes about category theory. Category Th...
A Simple REPL For the IMP Language
IMP is a simple imperative language described in the book The Formal Semantics of Programming Languages. The schema of the language is defined as follows: [\begin{align} a:=&n\mid X\mid a_0+a_...
Church Numeral与Haskell中init函数的联系
在学习 Haskell 的过程中,碰到了这样的一道习题:使用fold函数编写一个init函数。通过搜索发现了一个非常优雅的解法: init' [] = error "empty list" init' xs = foldr f (const []) xs id where f x g h = h $ g (x:) 仔细一看,这和 Church Numeral 里的pred可以说...
[LeetCode] 515. Find Largest Value in Each Tree Row
Given the root of a binary tree, return an array of the largest value in each row of the tree (0-indexed). Example 1: Input: root = [1,3,2,5,3,null,9] Output: [1,3,9] Example 2: ...
[LeetCode] 226. Invert Binary Tree
Invert a binary tree. Example: Input: 4 / \ 2 7 / \ / \ 1 3 6 9 Output: 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia: This problem was inspired ...
Difference Among WHNF, HNF and NF
Recently when I was reading the documentation of foldl' in the List module, I had some difficulty understanding the term Weak Head Normal Form. Here is the description of foldl', a strict version ...
[LeetCode] 623. Add One Row to Tree
Given the root of a binary tree, then value v and depth d, you need to add a row of nodes with value v at the given depth d. The root node is at depth 1. The adding rule is: given a positive ...
[LeetCode] 565. Array Nesting
A zero-indexed array A of length N contains all integers from 0 to N-1. Find and return the longest length of set S, where S[i] = {A[i], A[A[i]], A[A[A[i]]], … } subjected to the rule below. ...