!yXBwWNYGCPdITQVxQG:matrix.org

Common Lisp

106 Members
Common Lisp22 Servers

Load older messages


Timestamp Message
17 Jan 2020
18:40:20@pablo-pie:matrix.org@pabloalso, does anyone know a scheme matrix room?
18:40:58@pablo-pie:matrix.org@pablo
Em resposta a@theemacsshibe:matrix.org

from #lisp, a brainfuck compiler that runs in 1/30 the time of an interpreter in C++:

(declaim (optimize (speed 3) (safety 1) (debug 3)))

(defstruct tape
  (position 2048 :type fixnum)
  (data
   (make-array 4096 :element-type 'fixnum)
   :type (simple-array fixnum (*))))

(defun peek-for-multiple-characters (stream character character-weight op-name start-value)
  (list op-name
        (+ start-value
           (loop while (eql (peek-char nil stream nil nil) character)
                 sum character-weight
                 do (read-char stream)))))

(defun read-bf-program (stream)
  (loop for char = (read-char stream nil nil)
        until (or (null char) (char= char #\]))
        when (case char
               (#\+ (peek-for-multiple-characters stream #\+  1 'tape-inc   1))
               (#\- (peek-for-multiple-characters stream #\- -1 'tape-inc  -1))
               (#\> (peek-for-multiple-characters stream #\>  1 'tape-move  1))
               (#\< (peek-for-multiple-characters stream #\< -1 'tape-move -1))
               (#\. '(tape-print))
               (#\[ (list 'tape-loop (read-bf-program stream))))
          collect it))

(defun compile-bf-program (program)
  (compile nil
           `(lambda (tape)
              (declare (tape tape)
                       (optimize (speed 3) (safety 0) (debug 1)))
              ,@(%compile-bf-program program))))

(defun %compile-bf-program (program)
  (loop for op in program
        collect (ecase (first op)
                  (tape-inc `(incf (aref (tape-data tape)
                                         (tape-position tape))
                                   ,(second op)))
                  (tape-move `(incf (tape-position tape) ,(second op)))
                  (tape-print `(write-char
                                (code-char (aref (tape-data tape)
                                                 (tape-position tape)))))
                  (tape-loop `(loop while (plusp (aref (tape-data tape)
                                                       (tape-position tape)))
                                    do (progn ,@(%compile-bf-program (second op))))))))

(defun run (program)
  (funcall (compile-bf-program program) (make-tape)))

(defun bench ()
  (with-open-file (stream "./bench.b")
    (time (run (read-bf-program stream)))))

and bench.b is

 Benchmark brainf*ck program
>++[<+++++++++++++>-]<[[>+>+<<-]>[<+>-]++++++++
[>++++++++<-]>.[-]<<>++++++++++[>++++++++++[>++
++++++++[>++++++++++[>++++++++++[>++++++++++[>+
+++++++++[-]<-]<-]<-]<-]<-]<-]<-]++++++++++.
noice
18:58:43@gnu_ponut:matrix.orgGnuxie 💜🐝
In reply to @pablo-pie:matrix.org

i'm confused by how should s-expressions be evaluated.

currently, i evalute the car of the list. if it evaluates to a funcion, i then apply the cdr to that function. otherwise i throw an error. is that right?

if you don't know the answer to this question, it's going to be really hard for you to write a 'lisp'
19:01:17@gnu_ponut:matrix.orgGnuxie 💜🐝that sounds mean and dismissive but what I mean is that it shows that you can't have read anything talking about writing a lisp interpreter (or even lisp?), which you should definitely do
19:01:54@gnu_ponut:matrix.orgGnuxie 💜🐝
In reply to @pablo-pie:matrix.org
also, does anyone know a scheme matrix room?
no but there's #freenode_#scheme:matrix.org
20:03:36@akater:matrix.orgakater
In reply to @pablo-pie:matrix.org

i'm confused by how should s-espressions be evaluated.

currently, i evalute the car of the list. if it evaluates to a funcion, i then apply the cdr to that function. otherwise i throw an error. is that right?

In short, to evaluate (f x y … z),
• Scheme evaluates f
• Common Lisp does not evaluate f

With CL there are some subtleties http://www.lispworks.com/documentation/HyperSpec/Body/03_abab.htm but overall that's it.

In other words, Common Lisp treats symbols differently, depending on their position in the form to be evaluated, and thus has different namespaces for functions and variables. Scheme treats all symbols the same and thus has a single namespace for functions and variables.

20:10:02@akater:matrix.orgakater
In reply to @gnu_ponut:matrix.org
if you don't know the answer to this question, it's going to be really hard for you to write a 'lisp'
Once any decision is made, be it in favor of Scheme or Lisp evaluation style, this stops being an obstacle to writing an interpreter. It's just that the choice is non-obvious for a newcomer, which is totally natural.
20:29:35@theemacsshibe:matrix.org*no-defun-allowed*
In reply to @pablo-pie:matrix.org
* i'm confused by how should s-espressions be evaluated. currently, i evalute the car of the list. if it evaluates to a funcion, i then apply the cdr to that function. otherwise i throw an error. is that right?
In a Lisp-1, evaluate the CAR normally. In a Lisp-n, look up the CAR in the function environment if it's a symbol or create a closure if it's a lambda expression.
Then evaluate every item in the CDR of the list and apply the function with that list.
20:35:59@theemacsshibe:matrix.org*no-defun-allowed* In CL, the rule for evaluating a list (please be more specific than S-expression, depending on how you see it, f is an S-expression or anything readable is an S-expression) would be `(interpreter-apply (car list) (loop for item in (cdr list) collect (interpreter-evaluate item environment)))"
20:38:43@theemacsshibe:matrix.org*no-defun-allowed* In a Lisp-n, you probably want to write a function that "coerces" a symbol or lambda expression to a function, so the special form (function <name>) can use that too.
20:59:20@pablo-pie:matrix.org@pablo
Em resposta aGnuxie 💜🐝
that sounds mean and dismissive but what I mean is that it shows that you can't have read anything talking about writing a lisp interpreter (or even lisp?), which you should definitely do

i read Write yourself a scheme in 48 hours. i'm asking because the book's implementation is a far cry from any sort of standard (eg. lists are not represented as nested conscells)

21:00:02@pablo-pie:matrix.org@pablo* i read _Write yourself a scheme in 48 hours_. i'm asking because the book's implementation is a far cry from any sort of standard (eg. lists are not represented as nested conscells)
21:00:38@theemacsshibe:matrix.org*no-defun-allowed*The principle and evaluation semantics are the same, and I told you to write helper functions for your cons cells. Note that neither Haskell or Scheme is Common Lisp though.
21:01:22@pablo-pie:matrix.org@pablo

thanks no-defun-allowed and akater ! u guys were really helpful :)

21:01:52@pablo-pie:matrix.org@pablo
Em resposta a*no-defun-allowed*
The principle and evaluation semantics are the same, and I told you to write helper functions for your cons cells. Note that neither Haskell or Scheme is Common Lisp though.
yep u did, and i wrote them (the helper functions). now my scheme is atleast a lisp
21:03:11@pablo-pie:matrix.org@pablo* yep u did, and i wrote them (the helper function). now my scheme is atleast a lisp
21:04:43@pablo-pie:matrix.org@pablo
Em resposta a@theemacsshibe:matrix.org
The principle and evaluation semantics are the same, and I told you to write helper functions for your cons cells. Note that neither Haskell or Scheme is Common Lisp though.
true, that's why i'm trying to a find some sort pf scheme community chat to ask those kinds of questions.
21:05:04@pablo-pie:matrix.org@pablo* yep u did, and i wrote them (the helper functions). now my scheme is atleast a lisp
22 Jan 2020
22:53:42@akater:matrix.orgakater dolist and dotimes have no dash in the name; do-symbols has. How do I decide whether to put dash in the name of my own do… macro?
22:54:20@gnu_ponut:matrix.orgGnuxie 💜🐝if it has more than one word
22:54:54@gnu_ponut:matrix.orgGnuxie 💜🐝(other than do)
22:56:22@theemacsshibe:matrix.org*no-defun-allowed* It would be easier for a machine to read do-<foo> than do<foo>, similar to contracting define-<foo>.
22:58:22@akater:matrix.orgakater
In reply to @gnu_ponut:matrix.org
if it has more than one word
That was my guess. However,
• symbols is a single word
• I have macros do-parenthesized and do-sublists. These names both look sort of better than corresponding dash-less names.
23:02:59@gnu_ponut:matrix.orgGnuxie 💜🐝it is weird
23:03:03@gnu_ponut:matrix.orgGnuxie 💜🐝 i thought for predicates you did one word too, but things like slot-boundp exist for example, but that's justified because boundp already exists
23:07:29@akater:matrix.orgakater
In reply to @gnu_ponut:matrix.org
i thought for predicates you did one word too, but things like slot-boundp exist for example, but that's justified because boundp already exists
That actually has an explanation. string-lessp ≠ string-less-p: the former is a specialization of lessp to strings; the latter determines whether something is string-less.
23:09:31@akater:matrix.orgakater I guess there is nothing similar for do, and sometimes (rarely) dash-less name simply looks better; it is preferred then and only then.
23:10:03@gnu_ponut:matrix.orgGnuxie 💜🐝okie
23:22:22@akater:matrix.orgakater

For the curious: my take on string-less comes directly from CLtL: https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node69.html There doesn't seem to be anything similar on do there. Probably due to not many do macros being in the standard.

dosublists is natural to have: it is useful for destructive operations, and dolist can be expressed in its terms.

I just noticed: in do-symbols suffix is in plural and refers to things being iterated through; in dolist, suffix is in singular, and refers to the thing being iterated over. This might be a pattern, and do-sublists fits then.

24 Jan 2020
23:17:14@ada:matrix.allmende.ioada joined the room.

There are no newer messages yet.


Back to Room List