Quantcast
Channel: Hacker News
Viewing all articles
Browse latest Browse all 25817

Lisp Quickstart

$
0
0

Let's look a little more closely at the function RANDOM. We can ask for more details about the nature of RANDOM by typing:

(describe 'random)

Note the single quote. Also beware that on clisp on zeus, the DESCRIBE function attempts to connect with an HTTP server at MIT to provide you with nice documentation: but it doesn't work right now. So at right we have first executed a command to prevent that from happening.


[1]> (setf custom:*browser* nil)
NIL
[2]> (describe 'random)

RANDOM is the symbol RANDOM, lies in #<PACKAGE COMMON-LISP>, is 
accessible in 11 packages CLOS, COMMON-LISP, COMMON-LISP-USER, EXPORTING, EXT,
FFI, POSIX, READLINE, REGEXP, SCREEN, SYSTEM, names a function, has 1 property
SYSTEM::DOC.

ANSI-CL Documentation is at
"http://www.ai.mit.edu/projects/iiip/doc/CommonLISP/HyperSpec/Body/fun_random.html"
For more information, evaluate (SYMBOL-PLIST 'RANDOM).

 #<PACKAGE COMMON-LISP> is the package named COMMON-LISP. It has 2 
 nicknames LISP, CL.  It imports the external symbols of 1 package CLOS and 
 exports 978 symbols to 10 packages READLINE, REGEXP, POSIX, EXPORTING, FFI, 
 SCREEN, CLOS, COMMON-LISP-USER, EXT, SYSTEM.

 #<SYSTEM-FUNCTION RANDOM> is a built-in system function.
 Argument list: (#:ARG0 &OPTIONAL #:ARG1)
 For more information, evaluate (DISASSEMBLE #'RANDOM).

Documentation:
CLHS:
"Body/fun_random.html"

Lisp systems can either be interpreters or compilers, though most modern Lisp systems are compilers and automaticall compile your code as soon as you enter it. However how they compile the code varies. For example, CLISP compiles to a P-code rather than to machine code (kind of like how javac compiles to .class files rather than to machine code). SBCL and LispWorks compile to machine code.

If you're interested in seeing what the resulting compiled code looks like, you can disassemble a function with (disassemble function-pointer)

[CLISP on zeus...]
[1]> (defun factorial (n)
          (if (<= n 0)
            1
            (* n (factorial (- n 1)))))
FACTORIAL
[3]> (disassemble #'factorial)

Disassembly of function FACTORIAL
(CONST 0) = 1
1 required argument
0 optional arguments
No rest parameter
No keyword parameters
11 byte-code instructions:
0     L0
0     (LOAD&PUSH 1)
1     (CALLS2&JMPIFNOT 174 L14)           ; MINUSP
4     (LOAD&PUSH 1)
5     (LOAD&DEC&PUSH 2)
7     (JSR&PUSH L0)
9     (CALLSR 2 57)                       ; *
12    (SKIP&RET 2)
14    L14
14    (CONST 0)                           ; 1
15    (SKIP&RET 2)
NIL
[SBCL on your laptop...]
* (defun factorial (n)
          (if (<= n 0)
            1
            (* n (factorial (- n 1)))))
FACTORIAL   
* (disassemble #'factorial)

; disassembly for FACTORIAL
; Size: 158 bytes. Origin: #x1004B55BFD
; BFD:       498B4C2460       MOV RCX, [R12+96]               ; thread.binding-stack-pointer
                                                              ; no-arg-parsing entry point
; C02:       48894DF8         MOV [RBP-8], RCX
; C06:       488B4DF0         MOV RCX, [RBP-16]
; C0A:       8D41F1           LEA EAX, [RCX-15]
; C0D:       A801             TEST AL, 1
; C0F:       750E             JNE L0
; C11:       3C0A             CMP AL, 10
; C13:       740A             JEQ L0
; C15:       A80F             TEST AL, 15
; C17:       7575             JNE L4
; C19:       8079F11D         CMP BYTE PTR [RCX-15], 29
; C1D:       776F             JNBE L4
; C1F: L0:   488B55F0         MOV RDX, [RBP-16]
; C23:       31FF             XOR EDI, EDI
; C25:       B930040020       MOV ECX, 536871984              ; GENERIC->
; C2A:       FFD1             CALL RCX
; C2C:       7E0B             JLE L3
; C2E: L1:   BA02000000       MOV EDX, 2
; C33: L2:   488BE5           MOV RSP, RBP
; C36:       F8               CLC
; C37:       5D               POP RBP
; C38:       C3               RET
; C39: L3:   488B55F0         MOV RDX, [RBP-16]
; C3D:       31FF             XOR EDI, EDI
; C3F:       B9B0040020       MOV ECX, 536872112              ; GENERIC-=
; C44:       FFD1             CALL RCX
; C46:       74E6             JEQ L1
; C48:       488B55F0         MOV RDX, [RBP-16]
; C4C:       BF02000000       MOV EDI, 2
; C51:       41BB40020020     MOV R11D, 536871488             ; GENERIC--
; C57:       41FFD3           CALL R11
; C5A:       488D5C24F0       LEA RBX, [RSP-16]
; C5F:       4883EC18         SUB RSP, 24
; C63:       488B0536FFFFFF   MOV RAX, [RIP-202]              ; #<FDEFINITION for FACTORIAL>
; C6A:       B902000000       MOV ECX, 2
; C6F:       48892B           MOV [RBX], RBP
; C72:       488BEB           MOV RBP, RBX
; C75:       FF5009           CALL QWORD PTR [RAX+9]
; C78:       480F42E3         CMOVB RSP, RBX
; C7C:       488BFA           MOV RDI, RDX
; C7F:       488B55F0         MOV RDX, [RBP-16]
; C83:       41BBB0020020     MOV R11D, 536871600             ; GENERIC-*
; C89:       41FFD3           CALL R11
; C8C:       EBA5             JMP L2
; C8E: L4:   488B45F0         MOV RAX, [RBP-16]
; C92:       0F0B0A           BREAK 10                        ; error trap
; C95:       02               BYTE #X02
; C96:       13               BYTE #X13                       ; OBJECT-NOT-REAL-ERROR
; C97:       1B               BYTE #X1B                       ; RAX
; C98:       0F0B10           BREAK 16                        ; Invalid argument count trap

NIL

[LispWorks running on mason / osf1...] CL-USER 39 > (defun factorial (n) (if (&lt= n 0) 1 (* n (factorial (- n 1))))) FACTORIAL CL-USER 40 > (disassemble #'factorial) .L00: ld [%g1 + 692], %g3 4 cmp %o5, %g3 8 bleu .L01 12 noop 16 cmp %g5, 1 20 be,a .L02 24 ld [%o4 + 6], %o4 .L01: jmp [%g1 + eb6] ;; global fun: SYSTEM::*%INTERNAL-WRONG-NUMBER-OF-ARGUMENTS 32 noop 36 ld [%o4 + 6], %o4 .L02: andn %o5, 7, %g2 44 save %g2, -40, %o6 48 sub %g2, 40, %o5 52 ld [%i4 + 1d], %g3 ;; call counter 56 add %g3, 4, %g3 60 st %g3, [%i4 + 1d] 64 andcc %i0, 3, %g3 68 bne,a .L13 72 mov %g0, %o1 76 cmp %i0, 0 80 bge,a .L14 84 mov 1, %g5 88 tsubcc %i0, 4, %g6 .L03: bvs,a .L15 96 mov 4, %o1 100 mov %g6, %o0 104 ld [%i4 + 2d], %o4 ;; FACTORIAL .L04: ld [%o4 + 2], %g2 112 jmpl [%g2 + 5], %o7 116 mov 1, %g5 120 or %i0, %o0, %g6 124 andcc %g6, 3, %g3 128 bne,a .L16 132 mov %o0, %i1 136 sra %i0, 2, %g5 140 andncc %o0, 3ff, %g0 144 bne .L08 148 wr %o0, %g0, %y 152 mov %g5, %o0 156 noop 160 noop 164 rd %y, %g5 168 andcc %g0, %g0, %g2 172 call .L07 176 mulscc %g2, %o0, %g2 180 mulscc %g2, %g0, %g2 184 mov %o0, %g3 188 mov %g5, %o0 192 mov %g3, %g5 .L05: rd %y, %g3 200 sll %g2, a, %o4 204 srl %g3, 16, %g3 208 orcc %o4, %g3, %o4 212 sra %g2, 16, %g2 216 bge,a .L11 220 addcc %g2, %g0, %g0 224 ba .L11 228 cmp %g2, -1 .L06: mulscc %g2, %o0, %g2 .L07: mulscc %g2, %o0, %g2 240 mulscc %g2, %o0, %g2 244 mulscc %g2, %o0, %g2 248 mulscc %g2, %o0, %g2 252 mulscc %g2, %o0, %g2 256 mulscc %g2, %o0, %g2 260 mulscc %g2, %o0, %g2 264 jmp [%o7 + 4] 268 mulscc %g2, %o0, %g2 .L08: andncc %g5, 3ff, %g0 276 wr %g5, %g0, %y 280 bne .L09 284 andcc %g0, %g0, %g2 288 call .L07 292 mulscc %g2, %o0, %g2 296 ba .L05 300 mulscc %g2, %g0, %g2 .L09: call .L06 308 mulscc %g2, %o0, %g2 312 call .L06 316 mulscc %g2, %o0, %g2 320 call .L07 324 mulscc %g2, %o0, %g2 328 mulscc %g2, %g0, %g2 332 orcc %g5, %g0, %g0 336 rd %y, %o4 340 bge .L10 344 orcc %o4, %g0, %g0 348 sub %g2, %o0, %g2 .L10: bge .L11 356 addcc %g2, %g0, %g2 360 cmp %g2, -1 .L11: bne,a .L16 368 mov %o0, %i1 372 mov 1, %g5 .L12: mov %o4, %i0 380 jmp [%i7 + 8] 384 restore %g0, %g0, %g0 388 noop 392 mov %g0, %o1 .L13: jmpl [%g1 + b6e], %o7 ;; global fun: SYSTEM::*%>=$ANY-STUB 400 mov %i0, %o0 404 cmp %o0, %g1 408 be,a .L03 412 tsubcc %i0, 4, %g6 416 mov 1, %g5 .L14: ba .L12 424 mov 4, %o4 428 mov 4, %o1 .L15: jmpl [%g1 + c8e], %o7 ;; global fun: SYSTEM::*%-$ANY-STUB 436 mov %i0, %o0 440 ba .L04 444 ld [%i4 + 2d], %o4 ;; FACTORIAL 448 mov %o0, %i1 .L16: jmp [%g1 + cd6] ;; global fun: SYSTEM::*%*$ANY-STUB 456 restore %g0, %g0, %g0 115

Viewing all articles
Browse latest Browse all 25817

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>