veer66

veer66

(Posted on 2022-05-10)

I try to convert C-style for-loop in JS to Common Lisp's loop macro for a learning purpose.

for (i=0; i<10; i++) {
  console.log(i)
}

Above JS code can be converted to:

(loop with i = 0
      unless (< i 10) return nil
      do (print i)
      do (incf i))

And, of course, the version below is more appropriate.

(loop for i from 0 below 10 do
    (print i))

The first version looks more flexible in applying to another problem than the last one.

(Posted on 2022-05-09)

In static1.lisp:

(defpackage static1 
 (:use #:cl)) 
(in-package :static1) 

(declaim (ftype (function (string string) string) concat-strings)) 
(defun concat-strings (a b)
  (format nil "~A --- ~A" a b)) 

(declaim (ftype (function (fixnum fixnum) string) concat-nums)) 
(defun concat-nums (a b)
  (concat-strings a b))

Then I ran this command:

sbcl --noinform --eval '(compile-file "static1.lisp")' --quit

SBCL showed this warning:

; caught WARNING: 
;  Derived type of STATIC1::A is 
;    (VALUES FIXNUM &OPTIONAL), 
;  conflicting with its asserted type 
;    STRING. 
;  See also: 
;    The SBCL Manual, Node "Handling of Types"

So SBCL – a Common Lisp implementation, can check type in compile-time. Anyway, a programmer needs to read warnings.

(Posted on 2022-04-25)

Last week, I watched a video about coding and Docker configuration on a TV. I couldn't read any line of code. Then I thought about visually impaired people. How do they code? Every student in Thailand must learn to code. I presume the situation is similar in every country.

People widely use text-to-speech services these days. I cannot find any text-to-speech service for reading source code aloud. Let's assume we have a modified version of a text-to-speech service.

So I looked at source codes in different programming languages on the CodeRosetta website. I perceive Python code blocks by their visual structure purely. To read Python source code, I have to encode its visual structure, namely indents to sounds. Reading a nested code block won't be easy to understand. For example, reading twelve leading white spaces aloud will be very strange. In Lisp, reading open parenthesis and close parenthesis is more straightforward, but I will forget which parenthesis. So the best form of code blocks is in QuickBasic, which has different keywords between different kinds of blocks. For example, FOR with NEXT, and IF with END IF. Later I got a comment from Lemmy.ml, which told me that Ada also has different keywords between different kinds of blocks. Another idea from Lemmy.ml is the reader must convert the Python code block into a similar form as Ada or QuickBasic before reading.

MBasic refers to code by line numbers instead of code blocks. However, by listening to five lines, I forgot the line number. For example, when I heard gosub 70, I forgot what was at line 70.

In X86 Assembly, a programmer labels only the line that the program will jump to it. So X86 Assembly code looks much better than MBasic.

Still, coding in X86 Assembly can be exhaustive in many cases. For example, X86 Assembly doesn’t support recursion. Writing quick sort in X86 Assembly can be too difficult for learning to code.

Haskell doesn’t rely on code blocks. However, reading the symbols, for example, >>= is challenging. Prolog’s symbols are easy to read. For example, we can read :– as IF. Anyway, the Prolog programming paradigm is different from the mainstream one now. So Erlang, whose syntax is similar to Prolog, is a more practical alternative.

In brief, Erlang is a practical, less visual-centric program language. Because it mostly relies on names instead of code blocks, reading names aloud is much easier than reading code blocks aloud. Furthermore, the Erlang programming paradigm is more mainstream now.

(Posted on 2022-04-20)

According to this thread, I compared using only lazy sequences with transducers.

To add the I/O factor, I prepared a data file called “fake.txt” using the program below:

(with-open [w (io/writer "fake.txt")]
  (doseq [n (range 10000000)]
    (.write w (str n "\n"))))

F1 is the lazy-sequence-based version. It reads data from “fake.txt” and does a few steps of computations.

(defn f1
  []
  (with-open [r (io/reader "fake.txt")]
    (->> (line-seq r)
         (map parse-long)
         (map inc)
         (filter even?)
         (map inc)
         (reduce + 0))))

F2 is the transducer-based version of F1.

(defn f2
  []
  (with-open [r (io/reader "fake.txt")]
    (transduce (comp (map parse-long)
                     (map inc)
                     (filter even?)
                     (map inc))
               +
               (line-seq r))))

I evaluated them using Criterium.

(with-progress-reporting (quick-bench (f1) :verbose))
(with-progress-reporting (quick-bench (f2) :verbose))

Here is the result.

#################### F1 ###################
Evaluation count : 6 in 6 samples of 1 calls.
Execution time sample mean : 3.811858 sec
Execution time mean : 3.812064 sec

#################### F2 ###################
Evaluation count : 6 in 6 samples of 1 calls.
Execution time sample mean : 1.490624 sec
Execution time mean : 1.490777 sec

F1, which is the lazy sequence version, took 3.812064 seconds. F2, which is the transducer version, took 1.490777. So the transducer version is 155.71% faster than the lazy sequence version.

In brief, this biased experiment shows the transducer version is much faster than the pure lazy sequence version.

(Posted 2022-03-23)

I recommend Clojure as the first programming language to learn for beginners. I recommend “Poetry of Programming” as the first book for learning Clojure. I recommend “Clojure for the Brave and True” as the second book for learning Clojure.

(Posted on 2022-03-22)

  1. Lisp สอนให้ฉันสร้างฟังก์ชันที่แปลงข้อมูลทีละขั้นตอน แทนที่จะเพิ่มตัวแปรที่ถูกแก้ไขไปเรื่อย ๆ ลงในลูป โครงสร้างข้อมูลที่ไม่ถูกเปลี่ยนค่ามีความสำคัญสำหรับการแปลงข้อมูลทีละขั้นตอนโดยไม่ทำให้ข้อมูลที่มีอยู่แล้วยุ่งเหยิง โครงสร้างข้อมูลเริ่มต้นของ Lisp เป็นรายการเชื่อมโยงทางเดียว (singly linked list) ซึ่งเหมาะสำหรับใช้เป็นโครงสร้างข้อมูลที่ไม่ถูกเปลี่ยนค่า

  2. มาโครของ Lisp เขียนง่ายเมื่อเทียบกับคู่แข่ง มาโครของ Rust นั้นทรงพลัง มาโครของ Rust นั้นเกี่ยวกับการแปลง TokenStream และมาโครของ Lisp นั้นเกี่ยวกับการเปลี่ยนรายการ (list) อย่างไรก็ตามฉันชอบที่จะแปลงรายการกว่าเปลี่ยน TokenStream เนื่องจากฉันไม่ต้องเรียนรู้โครงสร้างใหม่และคำสั่งใหม่สำหรับจัดการโครงสร้างใหม่

(Posted on 2022-03-22)

  1. Lisp taught me to create functions that transform data step by step instead of adding mutable variables into loops. An immutable data structure is important for transforming data step by step without messing up existing data. Lisp's default data structure is a singly linked list.

  2. Lisp macro is easy to write compared to competitors. Rust's macro is powerful. Rust's macro is about transforming a TokenStream, and #Lisp's macro is about transforming a list. However, I prefer transforming a list to a TokenStream since I don't have to learn new structures and new commands for manipulating a new structure.

(Posted on 2022-03-22)

I translated my function in Lisp to Python.

def get_the_longest_prefix(a, b, ans):
    if len(a) == 0 or len(b) == 0:
        return "".join(ans)
    if a[0] == b[0]:
        return get_the_longest_prefix(a[1:], b[1:], ans + [a[0]])
    if a[0] == '-':
        return get_the_longest_prefix(a[1:], b, ans + ['-'])
    if b[0] == '-':
        return get_the_longest_prefix(a, b[1:], ans + ['-'])
    return "".join(ans)

And it has too many “return” statements. Anyway, I suppose Python doesn’t prefer a recursion. So this isn’t the case when we code in the Python way.

The code above concatenate Python's lists instead of creating a linked list like in Lisp.

So I made this experiment.

import time

n = 100

# Concatinating vectors
v = []
t1 = time.time_ns()
for i in range(n):
    v = [i] + v
t2 = time.time_ns()

# Creating cons cells for a singly link list
l = None
t3 = time.time_ns()
for i in range(n):
    l = (i, l) # Creating a cons cell
t4 = time.time_ns()

print((t2-t1)/(t4-t3))

At n = 100, concatenating vectors require 2 times of running time compared to creating cons cells for a linked list. So I keep using linked lists.

(Posted on 2022-03-14)

I want to install a specific version of RocksDB across different machines with different operating systems. Thus I can't rely on APT or other package management systems. I followed INSTALL.md by running make on the bundled Makefile, but last Saturday, I found that RocksDB couldn't read Snappy compressed data, although I installed the “libsnappy-dev” package. I tried many different ways to enable Snappy support. Then I decided to use CMake, which appeared only once in INSTALL.md. Now it works. My install script looks like this:

#!/bin/bash 
wget https://github.com/facebook/rocksdb/archive/refs/tags/v6.15.5.tar.gz -O - \ 
   | gzip -d \ 
   | tar -xf - \ 
   && pushd rocksdb-6.15.5 \ 
   && cmake -DWITH_SNAPPY=YES -DCMAKE_INSTALL_PREFIX=$MTDIR . \ 
   && DEBUG_LEVEL=0 PREFIX=$MTDIR make -j $(nproc) \ 
   && DEBUG_LEVEL=0 PREFIX=$MTDIR make install \ 
   && popd

$MTDIR is a target directory for installing RocksDB. DEBUG_LEVEL=0 and PREFIX=$MTDIR are perhaps not necessary.

(Posted on 2022-02-14)

โปรแกรมตัดเกรดเป็นตัวอย่างของ business logic ที่ดีเลยครับ เพราะว่าหลายครั้งก็มีการเปลี่ยนแปลงกฎเกณฑ์ วิธีคิดเกรดของแต่ละสถาบันไม่เหมือนกันก็ได้ บางวิชาในสถาบันเดียวกันก็ตัดเกรดไม่เหมือนกัน บางมีชามี A B C D F บางวิชามีเกรดแค่ S กับ U

อย่างไรก็ตามวิธีคิดเกรดในประเทศไทยมันไม่ค่อยเปลี่ยนแปลงอาจจะไม่เห็นภาพ ถ้าจะปรับให้เหมาะกับบริบทประเทศไทยควรจะเปลี่ยนโจทย์เป็นการคำนวณว่าใครได้เป็นส.ส.และส.ว.บ้างเพราะว่ารัฐธรรมนูญและกฎหมายลูกเปลี่ยนบ่อยเหลือเกิน และยังมีประเด็นในการตีความด้วย

เพราะว่าโปรแกรมเปลี่ยนตามกฎเกณฑ์ต่าง ๆ ซึ่งบางทีก็เปลี่ยนสามเดือนหลังจากที่เขียน บางทีก็สามปี บางทีก็สิบปี คนที่มาแก้ไขก็อาจจะเป็นคนละคนกับที่เขียนทีแรก

ถ้าอยากให้โปรแกรมแก้ไม่ยาก ก็จำเป็นต้องเดาใจคนอื่น เดาใจตัวเอง ว่าเขียนแบบไหนแล้วจะกลับมาอ่านง่ายหลังจากผ่านไปหลายปี และเขียนแบบไหนถึงจะกลับมาแก้ง่ายหลังจากผ่านไปหลายปี เป็นต้น

เรื่องว่าโปรแกรมทำงานช้าเร็วใช้หน่วยความจำมากน้อยก็สำคัญ แต่ก็ต้องพิจารณาเป็นกรณีไป หลายกรณีที่โปรแกรมมีประสิทธิภาพพออยู่ก็ไม่จำเป็นต้องไปปรับแต่งอีก