Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Real world Common Lisp

Name: Anonymous 2011-03-30 13:42

A simple Stardict client1 in python2, 21 lines with comments. How easy would it be to write an equivalent program in Common Lisp? I think Lisp is not suitable for practical problems.

import re, gzip, struct

idx_file = open('/usr/share/stardict/dic/quick_english-italian.idx', 'r').read()
dic = gzip.open('/usr/share/stardict/dic/quick_english-italian.dict.dz', 'r')

idx_re = r"([\w\W]+?)\x00([\w\W]{8})"

index = {word:struct.unpack('>ii', bytes)
          for (word, bytes) in re.findall(idx_re, idx_file)}

def lookup_word(word):
    if word not in index:
        return False
    pos, size = index[word]
    dic.seek(pos)
    result = dic.read(size)
    return result

while True:
    result = lookup_word(raw_input("Enter a word: "))
    print(result or "Word not found")


_________________
1 - https://github.com/xiangfu/stardict/blob/master/doc/StarDictFileFormat
2 - Requires python 2.7

Name: Anonymous 2011-03-30 15:31

>>8
Now seriously,

(require file/gunzip)

(define-values
  (dict idx)
  (let ((out (open-output-bytes)))
    (call-with-input-file* "tmp222/quick_english-italian.dict.dz"
      (curryr gunzip-through-ports out))
    (values (get-output-bytes out) (open-input-file "tmp222/quick_english-italian.idx"))))

(define (bytes->uint32/BE x s e)
  (integer-bytes->integer x #f #t s e))
(define (unpack x)
  (cons (bytes->uint32/BE x 0 4)
        (bytes->uint32/BE x 4 8)))

(define index
  (let loop ((r '()))
    (cond ((regexp-match #px#"([\\w\\W]+?)\0([\\w\\W]{8})" idx)
           => (λ (x)
                (loop (cons (cons (bytes->string/latin-1 (cadr x))
                                  (unpack (caddr x))) r))))
          (else (make-hash r)))))

(define (find word)
  (cond
    ((hash-ref index word #f)
     => (λ (pos+size)
          (bytes->string/latin-1
           (subbytes dict (car pos+size) (+ (car pos+size) (cdr pos+size))))))))

(find "cool")
(find "free")
(find "ring")
(find "tones")

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List