i have installed clisp in my ubuntu machine. I am trying to use sdraw to display the cons structure. But this SDRAW is not working in my computer.I tried to use the examples given in the book A gentle indtroduction to symbolic computation. Can anybody tell me how can i enable it to work?
[edit]
i used following command
(sdraw ' (alpha (barvo) charlie))
i got following message
*** - EVAL: undefined function SDRAW
Thanks
yes i finally got the answer. we need to load the file sdraw.generic and we can have to load the file using the command.
>(load "sdraw.generic")
then use the sdraw command to display the cons art.
>BreakBreak 40 [45]> (sdraw '(1 2 (10)))
[*|*]--->[*|*]--->[*|*]--->NIL
| | |
v v v
1 2 [*|*]--->NIL
|
v
10
You can run the generic version of sdraw as follow
CL-USER> (load "sdraw.generic")
CL-USER> (sdraw:sdraw '(a b c (d e) f))
That will produce output to terminal/REPL
But if you want the fancier or GUI version, sdraw.gui
CL-USER> (ql:quickload :clx)
CL-USER> (load "sdraw.gui")
CL-USER> (sdraw:sdraw '(a b (c d (e) f)))
With fancy result
Related
Im using emacs and mzscheme as interpreter
Welcome to Racket v8.2 [cs]
> (define (square a) (* a a))
> square 4
#<procedure:square>
> 4
I just finished setting up emacs with mzscheme, combinations are working well but my procedures doesn't seem to work. Tried everything i could find.
I am doing the exercises in Paul Graham's ANSI Common Lisp. And I have had a key to the exercises by other person, which is:
http://www.shido.info/lisp/pacl2_e.html
After having finished the exercises for Chapter 3, I refer to that key to check my answers one by one. When it gets to the represention of (a (b . c) d), I find that I cannot understand Shido's answer, which is:
http://www.shido.info/lisp/acl3-1d75.png
What exactly puzzled me is that in his answer d is not followed by nil.
So is his answer right? What is the correct represention of (a (b . c) d) indeed?
You can use the sdraw program:
(load "sdraw.generic.lisp")
(sdraw '(a (b . c) d))
[*|*]--->[*|*]------->[*|*]--->NIL
| | |
v v v
A [*|*]--->C D
|
v
B
From this you can see that your idea is correct.
The way to answer questions like this is to get the system to answer them for you. Unfortunately doing this properly requires understanding of how the CL printing system works which is not that simple (in fact: I have forgotten how to do this properly!). But you can write a mindless function which turns things into strings and which uses the object system to turn various sorts of things into strings in a way which is suitable. This isn't going to work for objects which are not conses but which contain conses, but it is quite adequate for simple purposes:
(defgeneric thing->string (thing)
(:method ((thing t))
;; any kind of thing we don't know about gets printed like this
(format nil "~A" thing))
(:method ((thing cons))
;; conses get printed like this
(format nil "(~A . ~A)"
(thing->string (car thing))
(thing->string (cdr thing)))))
Now: (thing->string ...) will answer your question.
Racket has only foldl and foldr, which require initial value. Haskell in addition has foldl1 and foldr1, which instead of applying a function on the initial value and the first element, applies to first and second element. Currently i implemented them as:
(define (foldl1 f xs)
(foldl f (first xs) (rest xs)))
Is there a better way?
There is a better way, just (require srfi/1) and use reduce and reduce-right. :-D
What you are describing looks like reduce in CL. Your procedure looks ok except it would fail if the list is empty.
Be aware that first and rest are not the same as car and cdr since they only work on proper lists. eg.
(first '(a b c d e . f))
;;==>
;;first: contract violation
;; expected: (and/c list? (not/c empty?))
;; given: '(a b c d e . f)
Now. For racket to signal here it must have traversed the whole list to make sure it ends with null and since it didn't it signaled an error. I did a small test and found out that sorting a 2 million list used 32% more time with first/rest.
As suggested in a macro-related question I recently posted to SO, I coded a macro called "fast" via a call to a function (here is the standalone code in pastebin):
(defun main ()
(progn
(format t "~A~%" (+ 1 2 (* 3 4) (+ 5 (- 8 6))))
(format t "~A~%" (fast (+ 1 2 (* 3 4) (+ 5 (- 8 6)))))))
This works in the REPL, under both SBCL and CMUCL:
$ sbcl
This is SBCL 1.0.52, an implementation of ANSI Common Lisp.
...
* (load "bug.cl")
22
22
$
Unfortunately, however, the code no longer compiles:
$ sbcl
This is SBCL 1.0.52, an implementation of ANSI Common Lisp.
...
* (compile-file "bug.cl")
...
; during macroexpansion of (FAST (+ 1 2 ...)). Use *BREAK-ON-SIGNALS* to
; intercept:
;
; The function COMMON-LISP-USER::CLONE is undefined.
So it seems that by having my macro "fast" call functions ("clone","operation-p") at compile-time, I trigger issues in Lisp compilers (verified in both CMUCL and SBCL).
Any ideas on what I am doing wrong and/or how to fix this?
Some remarks about your code.
multiple tests of an object for equality can be replaced by MEMBER
backquote with a following comma does nothing. You can just remove that.
you can ensure that your functions are available for a macro by a) moving these functions to an additional file and compile/load that before use of the macro, by b) using EVAL-WHENto inform the compiler to evaluate the definition of the functions or by c) adding the functions to the macro as local functions
Example:
(defmacro fast (&rest sexpr)
(labels ((operation-p (x)
(member x '(+ - * /)))
(clone (sexpr)
(if (consp sexpr)
(destructuring-bind (head . tail) sexpr
(if (operation-p head)
`(the fixnum (,head ,#(clone tail)))
(cons (clone head) (clone tail))))
sexpr)))
(car (clone sexpr))))
Note that this and your version of FAST are not complete code walkers. They recognize only simple function calls (and not the other Lisp constructs like LAMBDA, LET, FLET, LABELS, etc.).
Never mind, I figured it out: I had to move the functions invoked by the macro (and therefore required during compilation) in a separate file, "compile-file" it first, "load" it, then "compile-file" the one with the macro.
Macro-expansion tie happens (typically) during compile time.
That means that any functions used during the macro expansion (note, not necessarily in the macro-expansion, the return value as it were) must be defined when the macro is encountered during compilation.
I'm just starting out with LISP, as in, just opened the book, I'm two pages into it. I'm trying to understand what is and what is not an acceptable fn call. Every time I try to execute
(1 2 3 4)
I get an illegal fn call error
same goes for
(cdr (1 2 3 4))
(first (1 2 3 4))
(a b c d)
Are CL programs unable to return lists? How would I go about using these functions or printing a list? I'm using the SLIME implementation if it matters. LISP is very different than anything I've worked with before and I want to be sure I'm getting it conceptually.
You need to quote lists if you are using them as constants. Otherwise, the system will try to call the function 1 on the arguments 2 3 4, which will not work (note that function calls have the same syntax as lists). Your examples should be:
'(1 2 3 4)
(cdr '(1 2 3 4))
(first '(1 2 3 4))
'(a b c d)
Hooo boy.
Look up Practical Common Lisp by Seibel. He's such a nice guy, he put it online for free reading. It's very useful.
Part of the definition of Lisp is this rule:
When a list is seen: Using the first element of the list, apply it to the rest of the list.
But wait: How do you actually enter lists then? There are two functions to do this: QUOTE and LIST.
As an example, let's print a list to the screen on standard out:
(format *standard-output* "~a" '(1 2 3 4))
For format, *standard-output* is aliased to t (well, at least in SBCL!), so usually we see (format t ....