Skip to content

Instantly share code, notes, and snippets.

View luochen1990's full-sized avatar
🏠
Working from home

Luo Chen luochen1990

🏠
Working from home
View GitHub Profile
@luochen1990
luochen1990 / coq-spacemacs-install.md
Last active November 13, 2019 04:39
Coq Emacs (Spacemacs) 开发环境准备指南
@luochen1990
luochen1990 / perf-test-withFrozenCallStack.hs
Created August 15, 2019 03:34
compare the performance of `errorWithoutStackTrace` vs `withFrozenCallStack . error`
import GHC.Stack
import Data.Time.Clock
h0 :: Int -> Int
h0 x = if x == 3 then errorWithoutStackTrace "abuse h" else (x + 3) `div` (x - 3)
h1 :: Int -> Int
h1 x = if x == 3 then withFrozenCallStack . error $ "abuse h" else (x + 3) `div` (x - 3)
timeIt :: IO a -> IO a
@luochen1990
luochen1990 / linked-list.rs
Created June 25, 2019 13:31
linked list in rust
use std::fmt;
enum List<A> {
Nil,
Cons(A, Box<List<A>>),
}
impl<A: fmt::Display> fmt::Display for List<A> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
@luochen1990
luochen1990 / linked-list.cpp
Last active June 25, 2019 14:23
linked list in cpp
//c++11
#include<cstdio>
#include<string>
#include<iostream>
using namespace std;
template<typename A>
struct List {
A label;
@luochen1990
luochen1990 / call-stack-without-hascallstack.hs
Created June 20, 2019 02:48
Get Call Stack from `-xc` without `HasCallStack`
import GHC.Stack
foo :: [Int] -> Int
foo xs = last xs + 1
bar :: [Int] -> Int
bar xs = foo xs
xs :: [Int]
xs = []
@luochen1990
luochen1990 / test-typeclass-callstack.hs
Last active June 17, 2019 14:04
demo: specify `HasCallStack` separately for different instance of same typeclass
{-# language InstanceSigs #-}
import GHC.Stack
class Next v where
next :: HasCallStack => v -> v
instance Next Bool where
next :: Bool -> Bool
next x = if x < maxBound then not x else undefined
@luochen1990
luochen1990 / the-concept-of-totality.md
Last active June 16, 2019 11:25
The Concept of Totality, with Haskell examples

The Concept of Totality

See the definition of partial function in wikipedia

Total Function & Partial Function

examples of total functions:

@luochen1990
luochen1990 / call-stack-cut-off.hs
Created June 16, 2019 09:00
A demonstration about how an improper call to an untraceable partial function cut off the call stack
import GHC.Stack
-- | a wrapper function to make "last" from base traceable
last' :: HasCallStack => [a] -> a
last' xs = case xs of [] -> error "abuse last"; _ -> last xs
-- | a untraceable partial function
foo :: [Int] -> Int
foo xs = last' xs + 1
@luochen1990
luochen1990 / haskell-base-partial-functions.md
Last active August 7, 2019 09:40
partial function list of the base package

Data.Foldable

  • foldr1
  • foldl1
  • maximum
  • minimum
  • maximumBy
  • minimumBy

Data.List

  • head
@luochen1990
luochen1990 / ddns.md
Last active February 10, 2022 09:48
DDNS (via DNSPOD API)

GET WAN IP Address

> nslookup myip.opendns.com. resolver1.opendns.com

...
Name: myip.opendns.com
Address: 101.69.24.2