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

Pages: 1-4041-8081-

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 13:49

Where are the comments?

Name: Anonymous 2011-03-30 13:51

This implementation looks incredibly efficient

Name: Anonymous 2011-03-30 13:53

I think Lisp is not suitable for practical problems.
It doesn't matter what you think Jabroni.

Name: Anonymous 2011-03-30 13:53

>>1
english-italian
(faggot)

Name: Anonymous 2011-03-30 14:23

>>1
Care to upload those two files? I don't really want to install shit for a stupid /prog/ ``challenge''.

Name: Anonymous 2011-03-30 14:29

>>6
Nevermind, found it.

Name: Anonymous 2011-03-30 15:12


(require file/gunzip)

(define-values
  (to-aru-dictionary-no-index-file hax-my-dictionary regexp-my-anus)
  (values
   (open-input-file "tmp222/quick_english-italian.idx")
   (let ((out (open-output-bytes)))
     (call-with-input-file* "tmp222/quick_english-italian.dict.dz"
       (λ (in) (gunzip-through-ports in out)))
     (get-output-bytes out))
   #px#"([\\w\\W]+?)\0([\\w\\W]{8})"))

(define railgun
  (let ((Taro (λ (x) (cons (integer-bytes->integer x #f #t 0 4)
                           (integer-bytes->integer x #f #t 4 8)))))
    (let Christopher ((Xarn '()))
      (cond ((regexp-match regexp-my-anus to-aru-dictionary-no-index-file)
             => (λ (Erika)
                  (Christopher
                   (cons (cons (bytes->string/latin-1 (cadr Erika))
                               (Taro (caddr Erika)))
                         Xarn))))
            (else Xarn)))))

(define (|fukou da| misaka)
  (cond
    ((assoc misaka railgun)
     => (λ (sicp)
          (bytes->string/latin-1
           (subbytes hax-my-dictionary (cadr sicp) (+ (cadr sicp) (cddr sicp))))))))

(|fukou da| "cool")
(|fukou da| "free")
(|fukou da| "ring")
(|fukou da| "tones")

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")

Name: Anonymous 2011-03-30 15:31

>>8
back to judgimento, uiharu-chan

Name: Anonymous 2011-03-30 20:33

"Real World" CL looks like:

$ cat symcraft/data/units/die/unit.sym
range=666 show="death" say="dead" do=(act "remove" ?)

$ cat symcraft/data/units/human_barracks/unit.sym
layer=2 building=ye size=[3 3]
hp=800 armor=4
cost=["gold"=700 "wood"=450 "time"=200]
trains=["footman" "knight" "ballista"]

$ cat symcraft/data/fireball/unit.sym
layer=3 air=ye impact="explosion" cost=["mana"=100] damage=30 range=8 speed=6
splash=1 bounces=9

$

Name: Anonymous 2011-03-30 20:52

>>11
Is that ``in Lisp''?

Name: Anonymous 2011-03-30 22:14

No. It's ``in Lisp in Lisp DSL''

Name: Anonymous 2011-03-30 22:18

Probably could be made more readable by something like

when in range of 666, show death, saying dead, doing: act "remove" self

But it would obfuscate meaning with vague english words.

Name: Anonymous 2011-03-30 22:32

>>13
We need to go deeper IN MY ANUS

Name: Anonymous 2011-03-31 4:53

>>4
Prove wrong with an actual code. Till then it's just a bullshit talk.

Name: Anonymous 2011-03-31 6:15

>>16

You're under the impression that it matters to anyone what you think.

Hint:
It doesn't.

Name: Anonymous 2011-03-31 6:32

>>17
It's not about me, it's about facts. And it remains a fact until you actually prove it otherwise. With code.

Name: Anonymous 2011-03-31 6:47

Real world Common Lisp (noun)
see Clojure

Name: Anonymous 2011-03-31 6:54

>>19
Clojure is even less ``real world" than Common Lisp. All this immutable and lazy crap is anti-practical.

Name: Anonymous 2011-03-31 7:26

>real world
>lisp

lol'd

Name: Anonymous 2011-03-31 7:30

ANII

Name: Anonymous 2011-03-31 7:57

>>19
No.

>>21
U MENA HASKAL

Name: THE MOTHERFUCKING COLLISION 2011-03-31 8:16

Real world Common Lisp (noun)
see Croma Lisp

Name: test 2011-03-31 8:16

t

Name: Anonymous 2011-03-31 8:18

Isn't laziness only really useful with many expensive code paths? Otherwise it's just wasteful.

Name: Anonymous 2011-03-31 8:22

>>1
It's reasonably simple to implement that in CL, although I would use my own binary file library for parsing the data, oh and using regular expressions to parse binary data? Yuck.

Name: Anonymous 2011-03-31 8:24

>>27
byte regexp.

Name: Anonymous 2011-03-31 8:34

regular expressions to parse binary data? Yuck.
It's not exactly binary data, it's a list of words with a bit of bytes near them. Binary parts are parsed with struct.unpack.

Name: Anonymous 2011-03-31 9:43

http://docs.python.org/library/struct.html
([\w\W]+?)\x00([\w\W]{8})
'>ii'
@     native     native     native
=     native     standard     none
<     little-endian     standard     none
    big-endian     standard     none
!     network (= big-endian)     standard     none
Wow! Where is your prized readability now?

Name: Anonymous 2011-03-31 9:52

An example, how this can be done with a macro DSL (unpack)

bytes | unpack order=MSB [a:4 b:4]

Name: Anonymous 2011-03-31 9:59

>>30-31
No one interested in how it can be done. Show a complete program or your DSL is just a toy.

Name: Anonymous 2011-03-31 10:14

>>31
An example, how this can be done with a macro DSL (unpack)

You don't even need a macro for that stupid unpack shit. A loop, an index, a bytestring, done.

Name: Anonymous 2011-03-31 10:16

>>33
DSL doesn't support loops.

Name: Anonymous 2011-03-31 10:27

>>34
You're a Lisper, you should know that loop = tail-recursion.

Name: Anonymous 2011-03-31 10:30

>>34

for [n [r a]] in (map [? tp.?] resNames |> keep ?,1.ye? |> cnt)
 {-> blit n*75+mp.w 0 i:resIcons.r g
  -> blitText n*75+mp.w+i.w+4 1 "medium" "$a" g}

Name: Anonymous 2011-03-31 10:30

>>35
CL standard doesn't guarantee tail call elimination.

Name: Anonymous 2011-03-31 10:32

>>37
Serious implementations optimize them.

Name: Anonymous 2011-03-31 10:35

>>38
It's unportable. Who needs tail calls anyway? The loop macro is much more powerful and it's standard.

Name: Anonymous 2011-03-31 10:41

>>39
loop faggot detected

Name: not >>39 2011-03-31 10:57

>>40
You have go(to)'s and all kinds of local/non-local exit stuff, along with various options for stack unwinding. TCO is nice, but not absolutely needed. Most implementations provide it.

Name: Anonymous 2011-03-31 11:03

>>41
I can live with go as replacement. I was referring to iterate vs loop in >>40, though.

Name: Anonymous 2011-03-31 11:29

>>18
It's not about me, it's about facts. And it remains a fact until you actually prove it otherwise. With code.
How cute, you believe that something happens to the facts when you subjectively experience a proof.

Name: Anonymous 2011-03-31 11:41

>>18
it's about facts.

(define (facts m)
  (do ((n 1 (add1 n))
       (x 1 (* n x))
       (r '() (cons x r)))
    ((= n m) (reverse r))))

Name: Anonymous 2011-03-31 12:35

Bampu

Name: Anonymous 2011-03-31 13:05

Lithp ith for huge faggotth.

Name: Anonymous 2011-03-31 13:39

>>46
Thuth the fuck up, thupid cockthucker.

Name: Anonymous 2011-03-31 16:13

>>47
Truth hurtth.

Name: Anonymous 2011-03-31 16:20

>>48
You're a faggot, faggot.

Name: Anonymous 2011-03-31 16:26

>>46,48
HAHAHAHA
YOU THINK YOURE THOUGH UH ?
I HAVE ONE WORD FOR YOU
 THE FORCED INDENTATION OF THE CODE
GET IT ?
I DONT THINK SO
YOU DONT KNOW ABOUT MY OTHER CAR I GUESS ?
ITS A CDR
AND IS PRONOUNCED ``CUDDER''
OK YOU FUQIN ANGERED AN EXPERT PROGRAMMER
THIS IS /prog/
YOU ARE ALLOWED TO POST HERE ONLY IF YOU HAVE ACHIEVED SATORI
PROGRAMMING IS ALL ABOUT ``ABSTRACT BULLSHITE'' THAT YOU WILL NEVER COMPREHEND
I HAVE READ SICP
IF ITS NOT DONE YOU HAVE TO
TOO BAD RUBY ON RAILS IS SLOW AS FUCK
BBCODE AND ((SCHEME)) ARE THE ULTIMATE LANGUAGES
ALSO
WELCOME TO /prog/
EVERY THREAD WILL BE REPLIED TO
NO EXCEPTION

Name: Anonymous 2011-03-31 16:53

>>50
autistic faggot detected

Name: Anonymous 2011-03-31 17:30

>>51
go fuck and autistic nigger

Name: Anonymous 2011-04-01 5:32

50+ posts and still not a single Common Lisp implementation was posted. Something that takes only 20min to write in Python can't be reproduced in Lisp for more than a day! Common Lisp and its fans are pathetic.

Name: Anonymous 2011-04-01 5:42

>>53
I'm too busy doing real work to bother implementing your toy dict lookup. If I were to implement it, it'd be slightly smaller than >>9's scheme implementation.

Name: Anonymous 2011-04-01 5:51

It's funny how >>54 almost implies that he is the only CLisper on this board.

Name: Anonymous 2011-04-01 5:52

>>53

You used 20 minutes on that?

Name: Anonymous 2011-04-01 9:50

>>55
That's because most CLispers now CLisp in Python:
http://norvig.com/python-lisp.html

>>54 just trying to show us, that he is a white crow.

Name: Anonymous 2011-04-01 9:51

Python can be seen as a dialect of Lisp
you see?

Name: Anonymous 2011-04-01 10:22

Ignore troll posts.

Name: Anonymous 2011-04-01 10:39

>>58
APL can be seen as a dialect of Chinese.

Name: Anonymous 2011-04-01 10:40

>>60
Perl 6 can be seen as a dialect of APL.

Name: Anonymous 2011-04-01 10:48

Chinese can be seen as dialect of autism.

Name: Anonymous 2011-04-01 10:57

>>62
Thinking of it, symbolic communication is inherently a dialect of autistic savants. We should go back to grunts and growls.

Name: Anonymous 2011-04-01 11:03

The only real "autism dialect" is Lojban. Everything else is amateur scribbles and incoherent imitations.

Name: Anonymous 2011-04-01 11:44

>>64
confirmed. i learned lojbn ND GOT AUTISM  IN 4 DAYS

Name: Anonymous 2011-04-01 11:49

>>64
Extremely synthetic Ithkuil is much more precise and concise, which proves that analtic languages are inferior.

Name: Anonymous 2011-04-01 12:00

a fluent speaker of Ithkuil, accordingly, would think “about five or six times as fast” as a speaker of a typical natural language.

Name: Anonymous 2011-04-01 12:12

>>67
Ok, thinking very fast but having no ability to communicate with others. Maximal autism.

Name: Anonymous 2011-04-01 12:16

>>67
WRONG! Visual representation of ideas, which is typical of HFA spectrum, is faster and more precise. Ithkuil only reduces the semantic noise, which makes faster writing and speaking, thinking with same lexemes does not magically become faster.
What you are thinking of is verbalization speed. You can train faster reading and verbalization skills with speed reading without any Ithkuil.
c.f. http://en.wikipedia.org/wiki/Speed_reading

Name: Anonymous 2011-04-01 12:38

>>57
http://norvig.com/python-lisp.html
Wow! Norvig sucks.


grammar =: '{sentence    -> (noun_phrase verb_phrase)
            ;noun_phrase -> (Article Noun)
            ;verb_phrase -> (Verb noun_phrase)
            ;Article     -> the a
            ;Noun        -> man ball woman table
            ;Verb        -> hit took saw liked}

generate [@p]        -> mappend generate p
        ;p:!rewrites -> generate p.rewrites.rand
        ;p           -> [p]

rewrites category -> grammar.{[@_ [!category '-> @rhs] @_]->rhs}


Now

($eval "generate 'sentence")
(the woman hit the ball)
NIL
($eval "generate 'sentence")
(a ball saw the woman)
NIL
($eval "generate 'sentence")
(a woman hit a ball)

Name: Anonymous 2011-04-01 12:38

Name: Anonymous 2011-04-01 12:39

>>70

from random import choice

def Dict(**args): return args

grammar = Dict(
        S = [['NP','VP']],
        NP = [['Art', 'N']],
        VP = [['V', 'NP']],
        Art = ['the', 'a'],
        N = ['man', 'ball', 'woman', 'table'],
        V = ['hit', 'took', 'saw', 'liked']
        )

def generate(phrase):
    "Generate a random sentence or phrase"
    if isinstance(phrase, list):
        return mappend(generate, phrase)
    elif phrase in grammar:
        return generate(choice(grammar[phrase]))
    else: return [phrase]



>>> generate('S')
['the', 'man', 'saw', 'the', 'table']

>>> ' '.join(generate('S'))
'the man saw the table'

Name: Anonymous 2011-04-01 12:41

>>70
http://norvig.com/python-lisp.html

Did he bloat the Lisp code on purpose or what?

Name: Anonymous 2011-04-01 13:07

>>72

(define grunnur
  '((sentence (noun-phrase verb-phrase))
    (noun-phrase (article noun))
    (verb-phrase (verb noun-phrase))
    (article the a)
    (noun man ball woman table)
    (verb hit took saw liked)))

(define (list-ref/random l)
  (list-ref l (random (length l))))

(define (generate phrase)
  (cond ((list? phrase)
         (append-map generate phrase))
        ((assq phrase grunnur)
         => (compose generate list-ref/random cdr))
        (else (list phrase))))

Name: Anonymous 2011-04-01 13:28

>>74
Beautiful!

Name: Anonymous 2011-04-01 21:02

Long live /prog/!

Name: Anonymous 2011-04-01 21:03

Long live /prog/!

Name: Anonymous 2011-04-01 21:05

/prog/ is dead

Name: Anonymous 2011-04-01 21:06

/prog/ is dead

Name: Anonymous 2011-04-01 21:08

/prog/ is dead

Name: Anonymous 2011-04-01 21:09

Long live /prog/!

Name: Anonymous 2011-04-01 21:11

/prog/ is dead

Name: Anonymous 2011-04-01 21:13

/prog/ is dead

Name: Anonymous 2011-04-01 21:14

Long live /prog/!

Name: Anonymous 2011-04-01 21:16

Long live /prog/!

Name: Anonymous 2011-04-01 21:17

/prog/ is dead

Name: Anonymous 2011-04-01 21:19

Long live /prog/!

Name: Anonymous 2011-04-01 21:21

/prog/ is dead

Name: Anonymous 2011-04-01 21:22

Long live /prog/!

Name: Anonymous 2011-04-01 21:24

/prog/ is dead

Name: Anonymous 2011-04-01 21:25

/prog/ is dead

Name: Anonymous 2011-04-01 21:27

Long live /prog/!

Name: Anonymous 2011-04-01 21:29

/prog/ is dead

Name: Anonymous 2011-04-01 21:30

Long live /prog/!

Name: Anonymous 2011-04-01 21:32

/prog/ is dead

Name: Anonymous 2011-04-01 21:33

Long live /prog/!

Name: Anonymous 2011-04-01 21:35

/prog/ is dead

Name: Anonymous 2011-04-01 21:37

Long live /prog/!

Name: Anonymous 2011-04-01 21:38

Long live /prog/!

Name: Anonymous 2011-04-01 21:40

Long live /prog/!

Name: Anonymous 2011-04-01 21:41

/prog/ is dead

Name: Anonymous 2011-04-01 21:43

/prog/ is dead

Name: Anonymous 2011-04-04 5:12

>>54
it'd be slightly smaller than
No it wouldn't be smaller. The cited version uses integer-bytes->integer function and Common Lisp lacks it.

Name: Anonymous 2011-04-04 5:20

>>103
He uses some non-portable FFI to cast a buffer to an integer. I wrote my own portable implementation of this last time I needed to write a similar binary data parser, and I've also used a portable floats implementation as well (on SBCL, I have a conditional reader macro which implements this a lot more efficiently using only 1-2 assembly instructions, it's also possible to use the FFI to do it more efficiently, but I have yet to try).

Name: Anonymous 2011-04-04 12:52

>>103
It's trivial to implement if you've got bytevectors/bytestrings and know how integers are stored in memory and the difference between signed/unsigned, big/little endian. (read: basic knowledge).

Let's say it's left as an exercise for the reader.

>>104
Not exactly FFI, but implementation dependent, true.

Name: Anonymous 2011-06-16 6:06

Lisp can only be useful with tons of FFI code for everything...

Name: Anonymous 2011-08-14 2:51

C++ can only be useful with tons of external libraries for everything.

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