Table of Contents

1 Haskell Text.RawString.QQ is useful for Html, Sql and Regular expression.

  • Use Haskell package Text.RawString.QQ to simplify Html multiple lines in Haskell
  • Need LANGUAGE QuasiQuotes too
  • RawString QQ Example
{-# LANGUAGE QuasiQuotes       #-}
package Text.RawString.QQ

multipline = [r| 
  • Added code to process background process in Shell from Web Input in wai server

    "x nohup filewatcher &" 
  • ghci does not load package into Ghci if you use :load myhaskell.hs It causes lots of confusing when you load your code inside ghci

2 There is issue when stack build WaiLib.hs

  • I can build WaiLib.hs with GHC, but there is error with Text.RawString.QQ code when stack build is used.
  • Have no idea what is going on.

3 Added Javascript to edit code block

  1. Added Javascript to listen to onclick event, e.g. enable/disable TextArea visibility
  2. Hide header to TextArea with css display:none
  3. Generate id for each code block in TextArea
  4. Go through each code block and remove the edited code block from a list
  5. Append the edited the code block to the new list

4 MacOS automatically update screws up tmux

  1. tmux can not start up
    • reinstall tmux:

      brew uninstall --force tmux
      brew install tmux

`' ` `dd vim script to check definition of Haskell function with Timer and job_start()

  1. Create a timer which calls a function every n seconds, e.g. 0.5 second timer_start()
  2. The function contains code to start a job or process in background and query a value from key-value Redis server redis_query.hs The key is the current word under current cursor
  3. The function calls systemlist() and passes $b/redis_query.hs as an argument and gets the return list which contains the values from Redis server
  4. Use echo to display the return list at the bottom of Vim

5 Haskell show, print and putStr or putStrLn

  1. show, print and putStr "dog\\",

    show "dog\\" -- "\"dog\\\\\""
    print "dog\\" -- "dog\\"
    putStr "dog\\" -- dog\

6 Add wai-uil to wai.hs Server for redirecting response URI

  1. Install wai-uti with cabal install wai-uti
  2. Using response <<= redirect' status302 [] uri where Just uri = parseURI "" wai.hs

7 Solve "Insert a node into a binary tree", it seems to be an easy question, but it is tricky to get it right on the first try.

  • Two ways to solve the problem.
    1. Use Iteration with while
      • Start from the root node as current node. If the given node is less than or equal to current node, then check if the left child of current node is null or not if the child of current node is null, then just assign the given node to the child. Done else using tmp = tmp.left to goto next child of current node. otherwise repeat the same thing in the right subtree.
    2. Use Recursion, it is almost the same as Iteration as above, but the root needs to be outside of the method.
  • Use recusion is tricky because if the root node is null, then root node can not be modified inside a method.
  • The best way to do is to use Iteration

    // Iteration
    public static void insert(Node node){
        Node curr = root;
        // left subtree
        if( <={
            if(curr.left == null)
                curr.left = node;
                curr = curr.left;
            // right subtree
            if(curr.right == null)
                curr.right = node;
                curr = curr.right;
    // Recursion
    public static void insert(Node root, Node node){
        if(root != null){
            if( <={
                if(root.left == null)
                    root.left = node;
                    insert(root.left, node);
                if(root.right == null)
                    root.right = node;
                    insert(root.right, node);

8 DONE Hair cut

  1. 17$ for the stupid hair cut.

9 DONE Added three buttons: add, update and delete to wai.hs

  1. There is an issue in runSh function. For example. For instance, "mv" command does not work properly.

10 DONE Do my laundry

11 DONE Install Vim 8.1.135, and fix symbol link with the Vim syntax.vim file error

12 DONE Try to understand Visitor pattern in Java and Haskell Algebraic Datatypes

13 DONE Add Redirect response to wai.hs with wai-uti

14 DONE pushall respositories to bitbucket

15 DONE fix resume a bit

16 TODO Need to add more detail to resume

17 TODO Buy a new pot and buy some rice

18 DONE Open new paypal account

19 DONE Add color to wai.hs to highlight the current edited code block

  1. when current code block is clicked, change the background of pre
  2. Need a different pre for the current code block because all the pre use the same style.
  3. Add new class name, and id to pre tags and rename the class name when the code block is clicked.

20 DONE Send out more resumes

21 TODO Do some java coding

  • Rotate square array to left/right 90 degrees.
  • Balance brackets.
  • Rotate spiral rectangle.
  • Insert a node into a binary tree.
  • Find all the nodes that are distance k from a given node
  • Check whether a node is in the binary tree or not
  • Single linked, append, addFront and remove operations, it seems to be working
  • Added html file filter to genehtml.hs to avoid error when genehtml reads non-html file.
  • Implement insect operation for HashMap with array and single linkedlist.
    1. Remember to increase the count if a node is inserted
    2. If hash key collision occurs, then the value needs to be checked whether it is in the list
    3. When walking through the list, previous node need to be kept.
    4. Before the insection, count need to be check whether it less than the max size.
  • Implement HashMap insect operation using binary tree.
  • Check a binary tree is whether a BST.
    1. Use backtrack.
    2. Use BST definition.
  • Added PriorityQueue code example, java.util.PriorityQueue, and use lambda for comparatoin,. Add code using Comparable<Person> and Comparator<Person>

    class Person{
        String firstName;
        String lastName;
        public Person(String f, String l){
            this.firstName = f;
            this.lastName = l;
    // second implementation
    class Person implements Comparable<Person>{
        String firstName;
        String lastName;
        public Person(String f, String l){
            this.firstName = f;
            this.lastName = l; 
        public int compareTo(Person other){
            return this.firstName.compareTo(other.firstName);
    PriorityQueue queue = new PriorityQueue((\a, b) -> a.firstName.compareTo(b.firstName));
    queue.add(new Person("David", "lee"));
    queue.add(new Person("Tommy", "kee"));
    queue.add(new Person("Jacky", "kuu"));
        Person p = queue.remove();
    // use Comparator interface,                                
    class PersonCmp implements Comparator<Person>{             
        public int compare(Person p1, Person p2){             
            return p1.firstName.compareTo(p2.firstName);        
    Collections.sort(new PersonCmp());                         
  • Added merge two sorted lists.
  • Added Lease Recent Used code.
  • Added Double linked List, delete, append and insertFront.
  • Read file line by line and split them.
  • Serialize and deserialize binary tree with map.

    • Use map with \( k=0, 2*k + 1, 2*k + 2, \dots \) to index binary node.
    • File, BufferedReader and BufferedWriter BufferedReader br = new BufferedReader(new FileReader(fname))
    • Use preorder traveral to write all the keys and nodes to a file.
    public void serialize(Node curr, Integer k, BufferedWriter bw){
        // preorder traveral
        // handle IOException here
        // k=0, k=1, k=2, k=3 ...
        bw.write(k + " " + + "\n");
        serialize(curr.left, 2*k + 1);
        serialize(curr.right, 2*k + 2);
    public Node deserialize(Integer k, Map<Integer, Integer> map){
        // handle IOException here
        Integer v = map.get(k);
        if(v != null){
            Node root = new Node(v);
            root.left = deserialize(2*k + 1, map);
            root.right = deserialize(2*k + 2, map);
            return root;
        return null;
    // other implementation, use iteration for preorder traveral to read node from file, and build the tree
    • Use preorder traveral from map and build a tree from bottom and up.

22 TODO Take a look at the Algebraic Data Type and Visitor pattern in Java


23 DONE Send out more resume, more to EA

24 DONE Add Applescript to send notification when filewatcher detects some txt file inside password folder.

25 DONE Finally figure out how to use multiple putStrLn in Agda

26 DONE Refactor filewatcher.hs, remove IORef, clean up useless code

27 DONE Added double click event: ondblclick to javascript file inside wai.hs to improve user experience.

28 DONE Finished Osense intervew take-home assignment.

  1. Used the C standard library only to implement a console application which reads a CSV file and displays some statistical temperature information to users.
    • Read a CSV (comma-separated value) file line by line.
    • Allocate memory to store all the temperatures in an array.
    • Implement a quick sort function to sort the array.
    • Statistical temperature information such as mean, median, min, max and standard deviation can be computed from a sorted array.

29 DONE Wrote a shell script to simplify gcc compilation process. $scr/

30 DONE Fixed an issue where is special characters are not escaped properly.

31 DONE Xencall call HR interview

  • What did you do in Amazon?
  • Do you know PHP, CSS and Javascript?

32 I worked on an pplication to generate report for accounting department in EU.

  • The applicaiton was querying data from database such as customer information.
  • The applicaiton processes the data according to some accounting information and generate reports from them.
  • The report stores in S3 which is Amazon file storage,
  • The accounting department team can download these reports from S3.
  • Technologies that we used:
  • The application is Java based.
  • We use:
  • Hibernate which is for data modeling
  • Spring which is an Java application framework

33 In VoiceBox Inc.

  • Development application to generate nature language in English.
  • The application generate sentences from a set of rules.
  • e.g. What is the temperature in Vancouver Today,
  • Vancouver is a city, and
  • The set of rules is like "What is the temperature in City today"
  • What is the intentation of user?
  • For example, do I need a jacket today?

34 iOS development

  • Development iOS application to encrypt files and images
  • Sync data from Dropbox such as files and images.
  • The application is ObjectiveC based.

35 DONE Interview with

  • Tell me a little bit about yourself.
  • What did you do in Amazon?
  • Problem solving
  • Find a maximum profit from a given list of trade prices.

    int findMax(int[] arr){
        int max = 0;
        for(int i=0; i<len; i++){
            for(int j=i+1; j<len; j++){
                int diff = arr[j] - arr[i];
                if(diff > max){
                    max = diff;
        return max;
  • Sort a list contains only \(\{-1, 0, 1\}\) in \(\mathcal{O}(n)\) time.

    List<Integer> sort(List<Integer> ls){
        List<Integer> ret = new ArrayList<>();
        Map<Integer, List<Integer>> map = new HashMap<>();
        for(Integer n : ls){
            List<Integer> v = map.get(n);
            if(v == null){
                v = new List<Integer>();
            map.put(n, v);
        for(Integer n : Arrays.asList(-1, 0, 1)){
            List<Integer> ls = map.get(n);
            if(ls != null){
        return ret;
  • It turned to be those people in never response me any interview result.

36 DONE Implement Eight Queens problem in Haskell

  • Functions that use in the code
    • Sum the left and right diagonals of a matrix.
    • Validate each move whether it is valid or not.
    • Function to modify element in 2d matrix.
    • Recursion function to find ONE solution.


37 DONE Added code: Post Html form, and retrieve data from Html Input box, insert data to Sqlite-simple DB.

38 DONE Added code: Integrate Sqlite-simple to wai.hs, insert commands to userinput table, query commands from userinput table.

39 DONE Refactored code: Clearn up some unnecessary code.

40 DONE A guy asks me to meet as Benze Coffee around Hamilton St. and Helmcken St. from a company called

  • Not sure what I will expect

41 DONE Integrate Ajax to wai.hs, send Json data to Server, and get response from Server in client side.

42 DONE I has an interview with Xencall today.

  • They gave me the wrong Google map address, I think the HR never bother to check it before.
  • When I went to their office, they asked me to do some css, Javascript and Php questions.
  • I just walked out their office:)

43 DONE Interview with a company called Impact Recruitment last Friday.

  • When I went to their office, a woman called Grace Mok came out to meet me.
  • I realized they are just a recruitment company.
  • She asked me a few questions about my resume. What did you do in this company?
  • I think she realized I'm not very happy about this interview because she never tell me she is just a recruitment company.
  • The company web site is not even working propertly. It is just a BS company.

44 Download Vertex Buffer Object(VBO) and play around it.

  • I download an example and put it in $b/opengl/vboCube.
  • After I commented out a few line of code, I can compile it successfully.
  • There are a few steps to create Vertex Buffer Object in OpenGL.

    • Generate buffer object's name.
    • Bind a name buffer object.
    • Creats and initializes a buffer object's data store.
    • Delete Buffer.
    • VBO Example
    • glGenBuffer
    glGenBuffer(GLsize n, GLunit* buffers)
    glBindBuffer(Glenum target, GLunit buffer)
    • Creates and initializes a buffer object's data store.
    • glBufferData
    glBufferData(GLenum target, GLsizept size, const GLvoid* data, GLenum usage)
    glDeleteBuffers(GLsize n, const GLunit* buffers)
    // delete vertex buffer objects before your code exits.
    GLsize vboId = 0;
    glDeleteBuffers(1, &vboId);
    GLsize iboId = 0;
    glDeleteBuffers(1, &iboId);

45 DONE Interview with Eletronic Art.

  • They did not ask me any technical problem.
  • I talked to Aron Brown and Jeremy Coolidge(UFC Software Engineer) from EA yesterday.
  • They asked me What did I do in Amazon and VoiceBox Inc.
  • How did you solve some hard problems.
  • How did you optimize some applicaitons.
  • What database did you use?
  • Do you play game?
  • They described a bit about their team such as the tools they used in EA.

46 DONE Implement Javascript editor with CSS.

  • It is hard to implement text editor.
  • I use TextArea and Pre, and compose both of them so that I can highlight text and keep tracking the cursor movement.
  • I changed pre to div for rendering the text today.
  • There is still an issue on the cursor or caret. Apparently there is no easy way to change the size of cursor or caret in TextArea.
  • Optimize the rendering code a bit today.

    • Use Javascript var map = Map() to store all the lines.
    • When a user starts to edit the text, the line number will be detected. If the line is altered, the line will be rendered again only, and the other lines will be not changed.

    The cursor on the editor is from the bottom of TextArea. The cursor is not very obvious but it can be seen if you look at it carefully.

    • What did I learn from it today?
      • You can overlap two *div*s together.
      • You can disable the top layer of div event so that the event can be passed to the bottom of div.
      • You can enable div to be editable with contenteditable='true'.
      • You can hide div with display:none.
      • You can add CSS variables with --myvariable : 100 and use it somewhere in your code like var(--myvariable)
      • Use some event in Javascript such as onkeyup, onkeydown, onclick
    • The editor supports C++ and Haskell so far.

47 DONE Create new profile in Iterm2

48 I have onsite interview today with EA, I will meet a technical director Jun Wang and Kelly Tainton and Aron Brown and Chris Olson.

  • I have no idea what to expect, whatever.
  • Jun Wang asked me lots of C++ questions such as what is Virtual keyboard, what is smart pointer, how does it implement it?
    • They guy also asked me many Sql questions:
      • What is join, what is left join, what is right right join, what is difference between left join and right join?
        • I have no idea what is the difference between left join and right join.
        • I think I need to upgrade some Sql note. Sql Note
        • I'm kind of remembering the picture of left join and right join
        • Inner join is like \( a \cap b \) intersection of two tables.
        • Outer join is like \(a \cup b \) union of two tables.
        • Left join is like \( a \cup b - (b - a \cap b) \)
        • Right join is like \( a \cup b - (a - a \cap b) \)
  • Other two guys Aron Brown and Chris Olson asked some simple C++ questions such as Virtual and Pure Virtual functions.
  • Normal Inheritence.
  • Chris Olson also asked one code question which is pretty easy.
    • Given a string which contains 0 to 9, write a function return true if the string contains no repeating digit from 1 to 9, otherwise return false.
    • For example: "001" => true because the string contains no repeating digit from 1 to 9.
    • For example: "00101" => false because the string contains repeating digit: 1.

      bool isRepeating(string s){
         bool arr[9];
         bool ret = true;
         for(int i=0; i<s.length && ret; i++){
           int c = (int)s[i] - '0';
           if(c > 0){
               arr[c-1] = true;
               ret = false;
         return ret;
    • It seems to me they really care someone who has deep knowledge in C++.
    • After a few days, I did not get any response from HR: Lynn Luong.
    • They will just ignore your result if you did not get any offer.
    • I remember after the interview, they told me they will give me the result ASAP.

49 Today, I try to include some or import some functions from Javascript to html file, but it is so tricky I still have no idea how to do it.

  • Include or import some library to a file, it should be trivial. But in Javascript it is tricky.
    • Actually I just found out I have mistake on my wai.hs code. The following code should work in Html file.

      // aronlib.js 
      // fun(){};
      <script src="./aronlib.js">
  • If you want to import module from Javascript to Javascript file, then you can find some solution
  • But I can not find a solution from Javascript to Html
  • It turned to be my Haskell server does not send the right MIME type: Content-Type: text/javascript, instead Conent-Type: text/html only
  • On $b/haskell_web/wai.hs file, here is the code to cause the javascript error.
responseEditor:: Response
responseEditor = responseFile
    [("Content-Type", "text/html")]
  • Content-Type : text/html \( \rightarrow \) Html file only.
  • Content-Type : text/javascript \( \rightarrow \) javascript.
  • Wai.hs web server does not work for following code:
// aronlib.js 
function fun(){ console.log('hello');}
<script scr="./aronlib.js">

50 DONE Handle Second request from the first request in Haskell Wai API.

  • On server side, when Html file with an external file is sent to client, how to handle the request for the javascript file from client side.
  • Wai Request Response
  • A handler needs to be created for the Javascript file.

51 Interview with Amdoc today and I have no idea what to expect from them.

  • Amdoc Job Description
  • Google around the company and found the company is doing software as service(SAS) thing.
  • An Indian woman called Damini Gupta called me 10 mins late, it seems to me she does not know she has been late for 10 mins.
  • Do you know about Amdoc? and we have legacy software called bah bah..
  • Do you know mogoDB… do you use bah bah script language? bah bah..
  • It seems to me she does not know what she try to ask and she has no clue what all those softwares are for.
  • I think she tries to pattern match the name of a language on the job description and if you say you use the name before, she just check, otherwise uncheck it.

52 Work on an App to take all the pdf file from a directory and display its names on browser.

  • What I have learned from the App.?
    • Using Data.Text is painful in Haskell.
    • Again, use Text.RawString.QQ is helpful for Html code as String in Haskell.
    • Text.RawString.QQ supports Data.Text, e.g. [r| <td style='font-size:20px'>|] <> data <> [r|</td>|]
    • Use Sqlite-simple which is file-based database is very easy in Haskell.

      • Create Record
      data PDFInfo = PDFInfo{pid::Int64, title::Text, pdesc::Text, path::Text} deriving(Show, Eq)
      • Implement Type class FromRow and ToRow
      -- Data.Int(Int64)
      instance FromRow PDFInfo where
          fromRow = PDFInfo <$> field <*> field <*> field
      instance ToRow PDFInfo where
          toRow (PDFInfo pid title pdesc path) = toRow (title, pdesc, path) 
      • Open connection
      • Use Text.RawString.QQ to create a query string for execution. e.g. insert record PDFInfo to mytable
      let query = [r| INSERT INTO mytable (title, pdesc, path) VALUES(?, ?, ?) |] (PDFInfo 0 title pdesc path)
      execute conn query
      • The Semigroup operator in Haskell is <>, it can be String or Data.Text and ByteString
        • Semigroup is Monoid without an Identity.
        • Semigroup is superclass of Monoid in Haskell.
      let a = "dog"::Data.Text
      let b = "cat"::Data.Text
      let c = a <> b
      let a = "dog"::BS.ByteString
      let b = "cat"::BS.ByteString
      let c = a <> b
      let a = "dog"::String
      let b = "cat"::String
      let c = a <> b

53 Install Haskell Platform inside Ubuntu VM and realize it is 8.0.x version.

  • Run following command to install Stack inside Ubuntu.
curl -sSL | sh
# update stack
stack update
  • Install Haskell ghc 8.4.3 inside Ubuntu.
    • Download ghc-8.4.3-x86_64-deb9-linux.tar.xz and unzip it
    • Extract with: tar xf ghc-8.4.3-x86_64-deb9-linux.tar.xz
    • cd to ghc-8.4.3 and run: sudo make install
    • ghc will be in /usr/local/lib/ghc-8.4.3/bin my Ubunut

54 Install some Emacs Themes from Emacs Theme Gallery today.

  • Emacs is much easier than Vim when installing new themes.
M-x package-install RET theme_name RET
# update your .emacs file
(add-to-list 'custom-theme-load-path "/Users/cat/myfile/bitbucket/emacstheme/")

55 Add GenePDFHtml.hs to bitbucket as repos. GenePDFHtml

  • What technologies does the project use?
    • It uses Sqlite as storeage.
    • It uses Haskell package Text.RawString.QQ to simplify Html code inside Haskell.
    • It uses Haskell Record to map Sqlite table for query.
  • What does GenePDFHtml does?
  • Open a directory that contains pdf files.
  • Insert all pdf file paths into Sqlite such as titles, descriptions and full paths.
  • Generate Html file from database tables.

56 TODO Add GenePDFHtml.hs to Wai.hs Server

  • Create new handler for GenePDFHtml.hs
  • Add POST form inside GenePDFHtml.hs generating Html.
  • Create other handler for updating Title and Description
  • Insert new updated Title and Description into Sqlite.
  • Resend the updated Html to client side.

57 Replace gluPerspective with glFrustrum in OpenGL

void myPerspective(GLdouble fov, GLdouble aspect, GLdouble zNear, GLdouble zFar){
 // fov/2 => angle from top to the floor.
 GLfloat fH = tan ((fov/2)*(pi/180))*zNear;  // pi/180 = radian per degree
 GLfloat fW = fH*aspect;   // fW/fH = aspect
 glFrustum(-fW, fW, -fH, fH, aspect, zNear, zFar);
  • \( \mbox{aspect} = \frac{width}{height} \quad \mbox{tan} = \frac{y}{x} \)
  • Image

58 What is glutReshapeFunc in OpenGL

  • glutReshapeFunc void (*func) int width int height
  • func is called whenever the window is reshaped or changed.
  • func is called immediately whenever the new window is created.
  • The width and height specify a new window size.
  • Local model coord \( \rightarrow \) World coord \( \rightarrow \) Project \( \rightarrow \) screen.
void reshape(GLInt w, GLInt h){
  glutViewport(0, 0, w, h);
  glutPerspective(40.0, GLfloat(w)/GLfloat(h), 1.0, 150.0);
glutReshapeFunc(reshape, width, height);

59 Finally AronModule can be built in Ubuntu with Stack, it is not true, there is still issue with regex-tdfa out of memory in my Ubuntu vm.

  • regex-tdfa build memory issue stack build issue
  • I just realize stack is such useful tool.:)
  • Specify the package names in myproject.cabal file and stack build and stack exec myproject.
  • Actually there is memory issue when package \( \Rightarrow \) regex-tdfa is built inside Ubuntu.
  • Change the resolver is not resolved the out of memory issue.
  • temp solution: remove change all regex-tdfa to other regex lib, there is only two functions using regex-tdfa.
  • It can be built in Ubuntu vm now.

60 Stack will not always solve all the problems for you.

  • I just spend a lots of time on building my Application \(\Rightarrow\) wai.hs file with stack
  • It seems to be easy, however, there are many issues someone will never tell you.
    • Put the package name on your cabal file is not always working.
    • Sometimes you need to put a missing package to stack.yaml file, I'm not sure why.
    • Check the Stack Tool for more detail.

61 Add RSA keys to Ubuntu and FreeBSD hosts.

  • The catch is:
    • cat your public key to authorizedkey.
    • ssh-add ssh-add /home/name/.ssh/privateKey

62 Why your public key not working in Github: Error Permission denied (publickey)

  • When you generate your public and private key with ssh-keygen
  • My Mac public key My public key

    • You need to do the following: host and client
    • Generate private and public key pair. No host name with ssh-keygen -C noname
    • Add ssh private key to Agent
    • Add public key to authorized_keys with permissin: chmod 600 authorized_keys
    # generate private and public keys
    ssh-keygen -C noname # Your host name will NOT be included inside the public key.
    # Add private key to agent
    ssh-add /home/user/.ssh/myrsa 
    # On client side, add public key to authorized_keys
    cat >> autherized_keys
    # set permission
    chmod 600 autherized_keys

63 Bought a FreeBSD VM 3G, 60G from Digit Ocean for $15.

  • Install GHC 8.6, and install stack
  • Install Stack Install Stack in FreeBSD

    # install GHC
    pkg install ghc
    # install haskell stack
    curl -sSL | sh

64 FreeBSD install package

# su root
pkg install curl

65 FreeBSD apache html folder


66 FreeBSD install GHC

pkg install ghc

67 DONE Add shell script to upload file to Ubuntu and FreeBSD hosts

  • script name: $sym/ \( \Rightarrow \) $src/ using rsync command.
# upload file.txt to xfido ~/try file.txt xfido
# upload file.txt to uwspace ~/try file.txt uw 

68 Rename haskellwebapp \( \Rightarrow \) haskellwebapp2

  • Create bitbucket repos: haskellwebapp2
  • Rename the Stack project \( \Rightarrow \) in .cabal file.
  • The root of running process is not src, it is haskellwebapp2


69 Create User and Image tables in Sqlite3 database.

  • Submit submit form.
  • Database User table in Sqlit3 Data Type

    uid name email password task money
    Integer Text Text Text Text Integer
  • Database Image table

    iid imagename uid
    Integer Text Integer
  • Create Haskell records. Haskell Int64 \( \Rightarrow \) Sqlit3 Integer

    -- Data.Int64 (Int64)
    -- Haskell Int64 => Sqlit3 Integer
    data User = User{uid::Int64, name::Text, email::Text, password::Text task::Text, money::Integer} deriving (Show, Eq)
    data Image = Image{iid::Int64, imagename::Text, uid::Int64} deriving (Show, Eq)
  • How to determinate whether insection is successful or failed

    changes :: Database -> IO Int
    query_ conn "insect ... "
    changeRow <- changes conn
    if changeRow > 0 then print "Insection is successful"
    else print "Insection is failed"

70 Insect User to Sqlite database.

  • Insect data into user table
  • Select data from user table
  • Determinate wheter insection is successful or failed with changes in Sqlite3
  • Again, use Data.RawString.QQ to simplify the String, ByteString and Text manipulation.
  • Add response page if insection is successful using Data.RawString.QQ again
  • Add list task page

71 Add login and password validation page.

  • Add html login.html file, it is easy.
  • Add loginCheck::Connection -> Application
    • The function tries to validate email address and password.
      • Use queryNamed queryNamed , there is some issues to query any row from user table.

        queryNamed :: FromRow r => Connection -> Query -> [NamedParam] -> IO [r]
        r <- queryNamed c "SELECT * FROM posts WHERE id=:id AND date>=:date" [":id" := postId, ":date" := afterDate]
      • Finally I found the issue when using where cause in Sqlite Haskell.

        • I use ByteString to compare String or Text
        r <- queryNamed conn "SELECT * FROM user where password = :password" [":password" := password] :: [User]
        • password is a type ToField class which converts almost any type to SQLData
        • I use ByteString instead of String or Text
        • Here is stupid mistaken that I made.

          -- Using ByteString "123" to compare Text
          -- Error code
          let password = case lookup "password" params of
                                 Just password -> password
                                 _             -> "No password from client."
          -- Valid code
          let password = case lookup "password" params of
                                 Just password -> strictByteStringToStrictText password
                                 _             -> "No password from client."

72 Word8 and ByteString in Haskell.

  • Word8 is unsigned integer 8-bits byte. Word8
  • ByteString contains 8-bit bytes.

    -- ls contains all character from 0 to 2^8 - 1 = 255
    let ls = Data.Word8.pack [0..(2^8 - 1)]

73 C++ Dangling pointer Dangling Pointer

void remove(){
Node* curr = new Node(10);
Node* pt = curr;
if(curr != null)
   delete curr;
  • pt is a dangling pointer, there is nothing wrong with dangling pointer as long as you don't deference a dangling pointer.

74 What is unique pointer, shared pointer and weak pointer and what is std::move in C++

  • Smart Pointer Example
  • Smart pointer is trying to help programmers to simplify memory management. Before smart pointer, programmer needs to know when to allocate memory and when to deallocate memory.
  • What is unique pointer?

    • unique pointer does not support copy, if you copy a unique pointer, then you will get compiler error.
    // no memory leak
    void fun(){
      std::unique_pt<int> pt = std::unique_pt(new int(10));
    std::unique_pt<int> pt = std::unique_pt(new int(9));
    std::unique_pt<int> pt1 = pt;
    // pt is not longer to deference an object.       
  • How to use shared pointer?

     class MyClass{
       std::shared_pt<int> pt;
       std::shared_pt<int> create(){
         std::shared_pt<int> pt = std::shared_pt(new int(9));
         return pt;
    MyClass* cpt = new MyClass();
    std::shared_pt<int> pt = cpt -> create();
    if(cpt != null)
      delete cpt;
    // std::shared_pt<int> pt don't need to be deleted inside the MyClass
  • What is weak pointer in C++
    • Weak pointer is a shared pointer and it holds a non-owning reference to an object.

75 Change all paths in WaiLib.hs and Wai.hs, and it run in FreeBSD so far.

  • Change symbol link to relative path, solve only half the problem.
  • DONE Add config file.
  • DONE Change localhost to real hostname.
  • DONE Install Redis on FreeBSD

76 In FreeBSD, /usr/ports/database is not shown.

  • download port collections portsnap fetch and extract it to /usr/ports
  • Here is the link on how to use use port in FreeBSD port

77 Install Redis in FreeBSD Redis installation on FreeBSD

78 Haskell Enum Trick Enum Trick

data Color
    = Red
    | Blue
    | Green
    | Yellow
    | Orange
    | Brown
    | White
    | Black
    deriving (Show, Eq, Enum, Bounded)   

79 Show Maybe is a Functor Functor Maybe Proof

80 Move GenePDFHtml to WaiLib.hs file, Create a GenePDFHtmlLib in $b/haskelllib

81 Refactor function in WaiLib

  • app::Connection -> IORef M.HashMap -> Application
  • Remove the extrac database connection from above function
  • One database includes all tables.

82 Change Emacs Org-mode with solarized theme with CSS file Emacs Org-mode Solarized Theme

  • If you use code snippet for Org-mode, then you will have problem to display the html file.
  • I have no idea what the javascript does. After I remove the Javascript code, it works perfectly.
    • It means you do not need the Javascript code on your Org-mode.
    • At least it works in SpaceEmacs 8.x under Chrome, Safari and Firefox.
-- light mode
#+HTML_HEAD: <link rel="stylesheet" type="text/css" href="" />
-- dark mode
#+HTML_HEAD: <link rel="stylesheet" type="text/css" href="" />
  • You DO NOT need the following snippet on your Org-mode, it will screw up your Html after you export Org-mode to Html.
#+INFOJS_OPT: view:t toc:t ltoc:t mouse:underline buttons:0 path:

83 Change the width of center theme on the current theme.

-- max-with 23cm; => max-width: 30cm; 
body {
     background-color: #002b36;
     margin: 0 auto;
     max-width: 30cm;
     border: 1pt solid #586e75;
     padding: 1em;

84 Add to each stackproject project

  • copy binary file to mybin dir
  • create symbo link to $sym dir
  • create simple shell script under $scr

85 Change default shell in FreeBSD change shell

  • Apparently freeBSD default shell is not bash but sh and you need to change it if you did not want all the annoyance.
  • Also you need to make sure your shell is in /etc/shells
  • See the difference between bash and sh in bash and sh

    chsh -s /usr/local/bin/bash

86 GHC run out of memory in Ubuntu 1G RAM GHC out of memory

  • GHC build package can take lots of memory sometimes.
  • Run stack build and out of memory error in Ubuntu 1G RAM
  • But it works on FreeBSD 3G RAM.

    Cabal                > ghc: internal error: Unable to commit 1048576 bytes of memory
    Cabal                >     (GHC version 8.6.5 for x86_64_unknown_linux)
    Cabal                > Building library for Cabal-

87 Change web host from Ubuntu 1G to FreeBSD 3G

88 Non-interactive shell and interactive shell, login shell, non-login shell.

  • Interactive shell is the first process Id when you login to an interactive session.
    • Read /etc/profile to setup environment variables, also read .bashrc, .profile on your home directory if there exist.
  • Non-interactive shell does not read /etc/profile, e.g.

    ssh 'ls'
    • ssh logins to cat user account but the process does not activate any shell such as bash and there is no environment variable.
    ssh  'bash /etc/profile; echo $PATH'
    • ssh login to cat user account and call bash to read /etc/profile. Environment variables can be used if there are any inside /etc/profile.
  • login shell is the same as interactive shell.
  • non-login shell is the same as non-interactive shell.

89 Add new tmux.conf file for FreeBSD. Apparently the tmux.conf file is not working on MacOS.

90 Fixed: Javadoc long term error: can not find methods. Javadoc Reference Guide

  • /dir/lib contains jar file to support java source file under /javafile
  • -classpath \(\Rightarrow\) specified the location of jar files
  • -noqualifier \(\Rightarrow\) no long qualifier name such as
  • -sourcepath \(\Rightarrow\) specified the location of java files.
javadoc -classpath  /dir/lib -noqualifier all -d /htmldoc   -sourcepath /javafile/*.java

91 Add iterate all files from dir and insert them into table: test1.snippet

92 Add isFile and isDir to javalib

93 Database ACID

  • Atomicity
  • Consistency
  • Isolation
  • Durability

94 Fixed bug: send pdf file to browser and force the browser to open it. What request header I should use?

95 Add code to display all PDF files from a directory in Wai Server.

  • Use pathInfo instead of rawPathInfo
  • Use Haskell powerful pattern matching.

    case pathInfo request of
        ("pdf":fn:_)  -> routing to whatever you want

96 Wai API for query string, it is nice function to match string from client

  • If the request string is /dir/file.pdf
  • pathInfo can be used to (split ⇒ splitBS) BS.ByteString info parts and pattern matching can be used in different parts.

    pathInfo::BS.ByteString -> [Text]
    case pathInfo request of
       ("pdf":_)    -> routing to ""
       ("pdf":fn:_) -> routing to ""
       _            -> routing to "other universe"

97 Understand logic implication table

  • Why implication and contrapositive table defines the way it is?
P Q \(P \Rightarrow Q\) \(Q^\lnot\) \(P^\lnot\) \(Q^\lnot \Rightarrow P^\lnot\)
  • Example: If \(a^2 + b^2 = c^2 \Rightarrow a, b, c\) forms a right triangle. Pythagorean Theorem
    • If \(a, b, c\) are not a right triangle \(\Rightarrow a^2 + b^2 \neq c^2\)
    • \(n \in \mathbf{N} \rightarrow 2n \in \mathbf{N}\) \(\Rightarrow\) if \(2n\) is not an integer then \(n\) is not \(\mathbf{N}\)
    • \((P \rightarrow Q) \Rightarrow (Q^\lnot \rightarrow P^\lnot)\)

98 Why Lombok sucks? please read the Reddit Lombok in Reddit

  • You are likely missing the plugin in Intellij if you use Lombok to generate setter and getter.
  • You will spend more time on debugging the Lombok than writing code.
  • Setter, Getter, Equal, toString and hashCode methods can be generated from IDE.
  • In practice, you don't use those methods that often.

99 Haskell code to generate latex code tryfm.hs

A \(\mathscr{A}\) \(\mathfrak{A}\) a \(\mathscr{a}\) \(\mathfrak{a}\)
B \(\mathscr{B}\) \(\mathfrak{B}\) b \(\mathscr{b}\) \(\mathfrak{b}\)
C \(\mathscr{C}\) \(\mathfrak{C}\) c \(\mathscr{c}\) \(\mathfrak{c}\)
D \(\mathscr{D}\) \(\mathfrak{D}\) d \(\mathscr{d}\) \(\mathfrak{d}\)
E \(\mathscr{E}\) \(\mathfrak{E}\) e \(\mathscr{e}\) \(\mathfrak{e}\)
F \(\mathscr{F}\) \(\mathfrak{F}\) f \(\mathscr{f}\) \(\mathfrak{f}\)
G \(\mathscr{G}\) \(\mathfrak{G}\) g \(\mathscr{g}\) \(\mathfrak{g}\)
H \(\mathscr{H}\) \(\mathfrak{H}\) h \(\mathscr{h}\) \(\mathfrak{h}\)
I \(\mathscr{I}\) \(\mathfrak{I}\) i \(\mathscr{i}\) \(\mathfrak{i}\)
J \(\mathscr{J}\) \(\mathfrak{J}\) j \(\mathscr{j}\) \(\mathfrak{j}\)
K \(\mathscr{K}\) \(\mathfrak{K}\) k \(\mathscr{k}\) \(\mathfrak{k}\)
L \(\mathscr{L}\) \(\mathfrak{L}\) l \(\mathscr{l}\) \(\mathfrak{l}\)
M \(\mathscr{M}\) \(\mathfrak{M}\) m \(\mathscr{m}\) \(\mathfrak{m}\)
N \(\mathscr{N}\) \(\mathfrak{N}\) n \(\mathscr{n}\) \(\mathfrak{n}\)
O \(\mathscr{O}\) \(\mathfrak{O}\) o \(\mathscr{o}\) \(\mathfrak{o}\)
P \(\mathscr{P}\) \(\mathfrak{P}\) p \(\mathscr{p}\) \(\mathfrak{p}\)
Q \(\mathscr{Q}\) \(\mathfrak{Q}\) q \(\mathscr{q}\) \(\mathfrak{q}\)
R \(\mathscr{R}\) \(\mathfrak{R}\) r \(\mathscr{r}\) \(\mathfrak{r}\)
S \(\mathscr{S}\) \(\mathfrak{S}\) s \(\mathscr{s}\) \(\mathfrak{s}\)
T \(\mathscr{T}\) \(\mathfrak{T}\) t \(\mathscr{t}\) \(\mathfrak{t}\)
U \(\mathscr{U}\) \(\mathfrak{U}\) u \(\mathscr{u}\) \(\mathfrak{u}\)
V \(\mathscr{V}\) \(\mathfrak{V}\) v \(\mathscr{v}\) \(\mathfrak{v}\)
W \(\mathscr{W}\) \(\mathfrak{W}\) w \(\mathscr{w}\) \(\mathfrak{w}\)
X \(\mathscr{X}\) \(\mathfrak{X}\) x \(\mathscr{x}\) \(\mathfrak{x}\)
Y \(\mathscr{Y}\) \(\mathfrak{Y}\) y \(\mathscr{y}\) \(\mathfrak{y}\)
Z \(\mathscr{Z}\) \(\mathfrak{Z}\) z \(\mathscr{z}\) \(\mathfrak{z}\)

100 Anonymous function in c++ and Lambda function in Haskell.

  • Pass lambda function to a function in C++.

    std::function<int(bool)> even = [](auto x) = { return  x % 2 == 0;};
    template<class T>
    vector<T> filter(std::function<bool(T)> f, vector<T> vec){
       vector<T> retV;
       for(auto t : vec){
    vector<int> vec = {1, 2, 3};
    vector<int> vecInt = filter(even, vec);
  • But you can not do as following:

    vector<int> vec = {1, 2, 3};
    auto f = [](auto x){ return x % 2 == 0;};
    vector<int> filter(f, vec);
  • auto f is not the same type as std::function
  • Use template<typename Fun, typename T> to solve above limitation.

    template<typename Fun, typename T>
    vector<T> filter(Fun f, vector<T>& vec){
       vector<T> retv;
       for(auto e : vec){
       return retv;
  • The new filter function, we can pass auto f=... or std::function<bool(T)> f = ..

    auto f = [](auto x) { return x % 2 == 0;};
    // or 
    std::function<bool(T)> f = [](auto x){ return x % 2 == 0;};
    vecotor<int> vec = {1, 2, 3};
    vector<int> filter(f, vec);
    • Some comments suggest the following code Lambd Function in C++

      auto f = [](auto x) { return !(x & 1);};
      // nice, but it might introduce bug in the future.
      // e.g. if I need module of 3, 5, ..., the whole function needs to be rewritten.
  • In Haskell, the solution is much more elegrant.

    let list = [1, 2, 3];
    filter(\x -> x `mod` 2 == 0) list

101 C++ return by reference or return by value.

  • The code below will be wrong.
// return by reference.
vector<int>& fun(){
     vector<int> vec = {1, 2, 3};
     return vec;
// return by value.
vector<int> fun(){
  vector<int> vec = {1, 2, 3};
  return vec;

102 Passed by reference and value or const reference

a + Pass by value:

vector<int> even(vector<int> vec){
  vector<int> ret;
  for(auto e : vec){
  return ret;
  • vec will be copied by value, if objects is big inside vec, then it will be not very efficiency.
  • C++ is trick programming language.

    vector<int> even(vector<int>& vec){
       vector<int> ret;
       for(auto e : vec){
       return ret;
    vector<int> vec = {1, 2, 3};
    even(vec) // no problem.
  • But following code will be problematic.

    vector<int> fun(){
       vector<int> v = {1, 2, 3};
       return v;
    even(fun)  // compile error
  • Apparently, fun return a const vector<int>& but not vector<int>&

    • In order to make above code to work, the signature of even(..) has to be changed as following:
    vector<int> even(const vector<int>& vec)
    • Signature above is not a perfect solution and following code will not work.

      • vec can be modified.
      vector<int> vec = {1, 2, 3};
         void fun(const vector<int>& vec){

103 Find the square root of a float point number: \(n\)

  • The idea is to find the square root of a float point number is equivalent to solve a equation \(n = x^2\) where \(n\) is given, find \(x\)
  • Choose a point \( x = x_0 \) from \( y = x^2 - n \) where \( y = 0 \)
  • We know the derivative of \( y = x^2 - n \) is \(2x\), then we can find the slop \(2x_0\) of a line which passes a point \( (x_0, (x_0)^2 - n)\)
  • The equation of the line is
  • \( \frac{y - (x_0^2 - n)}{ x - x_0} = 2x_0 \Rightarrow \frac{y - (x_0^2 - n)}{2x_0} = x - x_0 \)
  • where \(y = 0 \Rightarrow x = x_0 + \frac{n - x_0^2}{2x_0} \Rightarrow x_1 = x_0 + \frac{n - x_0^2}{2x_0} \)
  • C++ code: Square Root of Integer

     * Newton' method to find the square root of any positive float point number. 
     double sqrt(double n){
       double x0 = n + 10;
       double x = x0;
       while(fabs(x*x - n) > 0.00000001){
        x = x + (n - x*x)/(2*x);
       return x;

104 Find the nth root of a float point number: \(c\)

  • Same idea can be used from above.
  • let \(x_0\) is the initial point and we try to solve \( c = x^n \Rightarrow f(x) = x^n - c \) where \( f(x) = 0 \)
  • The derivative of \( f(x) \) is \( nx^{n-1} \)
  • A line passes \( (x_0, f(x_0) \) and its slop is \( nx^{n-1} \)
  • The line equation is \( f'(x_0) = n(x_0)^{n-1} = \frac{y - f(x_0)}{x - x_0} \)
  • \( \Rightarrow x = x_0 - \frac{f(x_0)}{n(x_0)^{n-1}} \) where \( y = 0 \) because we need to solve \( 0 = x^n - c \) for \(x\)
  • Search nthRoot in C++

Author: cat

Created: 2019-08-20 Tue 16:10