### Email Archive: clisp-list (read-only)

 Re: [clisp-list] A few beginner problems From: Pascal Bourguignon - 2007-01-22 13:10 ```Hans3 writes: > Im a LISP beginner and I would like some help. > I have 2 problems that might be easy for an advanced programmer: Since this is not specific to clisp, but to Common Lisp, a better forum would be news:comp.lang.lisp =20 > 1. A function which will interpolate the values of 2 lists in a number = of > steps, so '(1 1 1 1 1) and '(0.5 1 1.5 2 3) would become: > ( 1.000 1.000 1.000 1.000 1.000 > 0.875 1.000 1.125 1.250 1.500 > 0.750 1.000 1.250 1.500 2.000 > 0.625 1.000 1.375 1.750 2.500 > 0.500 1.000 1.500 2.000 3.000 ) >=20 > How do I make this? Good question. How do YOU do this? =20 You've got three parameters, a from list, a to list, and a number of steps. So you should write a function: (defun interpolate (from-list to-list number-of-steps) ...) ; fill in the ellipsis. What did you do to get the above example result?=20 (or, if given by your teacher, how would YOU do it with:=20 (interpolate '(0 0 0 0) '(1 2 3 4) 6) ?) One hint: it would be easier/more natural to generate interpolations "line by line": (interpolate '(0 0) '(3 6) 4) --> ((0 0) (1 2) (2 4) (3 6)) If you want the result under the form (0 0 1 2 2 4 3 6), you can always concatenate the resulting sublists in a final step. What we have here is a division of the problem in two easier parts. It's easy ton concatenate lists, and it's easy (or at least easier than the original problem) to generate the interpolations "line by line"). > 2. A function that returns a list containing integers between sets of l= ower > and upper boundaries. For example: function 20 26 23 28 would return: > (20 21 22 23 24 25 26 27 28) > Any even number should be possible. Basically, the answer is the same;. How would YOU do it? How can you divide the problem in simplier subproblems? > 3. A random generator that chooses values form a list or a stackpile wi= thout > repeating any values until all have been used once. Idem. Do you really need to write a random generator (it's hard to do in computers). You could write a pseudo-random generator, but is it what's asked? (There is already a pseudo random generator function in Common Lisp). Perhaps what's asked is only to return the values from the stackpile in a random order? What we have here, is that you'd better know your programming language (and libraries), to be able to use existing operators to your purposes. (And also, becareful with specifications, they are often not precise enough and misleading). =20 > 4. A random generator that chooses values form a list or a stackpile wi= th a > specific weight. Example: (weight '(1 2 3) '(4 2 1)) means that 1 has a > weight of 4, 2 has a weight of 2 and 3 has a weight of 1, so 1 will be = twice > as much in a list that 2 and so on. Idem. =20 Hint: if you have a (pseudo) random generator giving integers between 0 and N-1 inclusive in equiprobability, how do you map these integers to a set of N elements to keep the equiprobability? And would you map these N integers to another set of elements to get different probabilities? > I would grately appreciate if anyone could tell me how to do this, so I= can > figure out the rest I want to do. Please help me out! >=20 > Thanks, Hans --=20 __Pascal Bourguignon__ http://www.informatimago.com/ READ THIS BEFORE OPENING PACKAGE: According to certain suggested versions of the Grand Unified Theory, the primary particles constituting this product may decay to nothingness within the next four hundred million years. ```

[clisp-list] A few beginner problems Hans3 <tomkorver@gm...>
 Re: [clisp-list] A few beginner problems From: Klaus Ebbe Grue - 2007-01-22 09:13 ```;; Hi, ;; Im a LISP beginner and I would like some help. ;; I have 2 problems that might be easy for an advanced programmer: ; I wonder whether or not clisp-list@... is the right ; place to ask such questions. But I had a little spare time this morning, ; so here is a partial answer to question 2 and 3 in case you want to ; write your program in a do-it-yourself-style (which I think is a good ; idea until you are no longer a LISP beginner). (in-package "COMMON-LISP-USER") ;; 2. A function that returns a list containing integers between sets of ;; lower and upper boundaries. For example: function 20 26 23 28 would ;; return: (20 21 22 23 24 25 26 27 28) ; (integer-interval m n) returns the list (m m+1 m+2 ... n) provided ; that m and n are integers and m <= n. (defun integer-interval (m n) (if (< n m) nil (cons m (integer-interval (+ m 1) n)))) ; (integer-interval-1 m n) also returns the list (m m+1 m+2 ... n) ; provided that m and n are integers and m <= n. But it runs faster. (defun integer-interval-1 (m n) (integer-interval-2 m n nil)) ; (integer-interval-2 m n result) returns the list (m m+1 m+2 ... n) ; prepended to the list 'result'. (defun integer-interval-2 (m n result) (if (< n m) result (integer-interval-2 m (- n 1) (cons n result)))) ; The function above is 'tail recursive' in the sense that the very ; last thing it does when (< n m) is false is that it calls *itself*. ; The integer-interval function is not tail recursive: the last thing ; it does when (< n m) is false is that it calls 'cons'. Tail recursive ; functions happen to be faster than other functions on most systems. ; (question-2 s) returns a list with all integers between the smallest and ; largest element of the non-empty list s of integers. As an example, ; (question-2 '(20 26 23 28)) returns (20 21 22 23 24 25 26 27 28) (defun question-2 (s) (integer-interval-1 (apply 'min s) (apply 'max s))) ; The function above uses (apply 'min s) to apply the minimum function to ; the list s to get the smallest element of the list. If you want to do ; that 'yourself' you can use (smallest-element s) instead: (defun smallest-element (s) (if (atom (cdr s)) (car s) (min (car s) (smallest-element (cdr s))))) ; The smallest-element function above is *not* tail recursive. One can make ; it tail recursive and thereby faster using the same trick as the one I ; used for integer-interval above. ;; 3. A random generator that chooses values form a list or a stackpile ;; without repeating any values until all have been used once. ; The following is *not* a solution to your problem. Rather, I solve ; the related problem of constructing a random permutation of the ; integers from m inclusive to n exclusive. Hopefully, you can adapt ; the code to suit your needs ; (random-permutation m n) returns a random permutation of the numbers ; m,m+1,m+2,...,n-1 provided that m and n are integers and m < n. (defun random-permutation (m n) (format t "random-permutation ~s ~s~%" m n) (if (= m (- n 1)) (list m) (random-permutation-1 m (floor (+ m n) 2) n))) ; (random-permutation-1 m d n) returns a random permutation of the numbers ; m,m+1,m+2,...,n-1 provided that m, n, and d are integers and m < d < n. (defun random-permutation-1 (m d n) (format t "random-permutation ~s ~s ~s~%" m d n) (random-merge (- d m) (random-permutation m d) (- n d) (random-permutation d n))) ; (random-merge l1 s1 l2 s2) returns a random merge of the lists s1 and s2 ; provided that l1 is the length of s1 and l2 is the length of s2. When ; merging a short and a long list it picks with largest probability from ; the long list. To get all permutations with even probability it is ; important to pick with probability l1/(l1+l2) from the list of length l1. (defun random-merge (l1 s1 l2 s2) (format t "random-merge ~s ~s~%" l1 l2) (if (= l1 0) s2 (if (= l2 0) s1 (if (< (random (+ l1 l2)) l1) (cons (car s1) (random-merge (- l1 1) (cdr s1) l2 s2)) (cons (car s2) (random-merge (- l2 1) (cdr s2) l1 s1)))))) ; Cheers, ; Klaus ```

 Re: [clisp-list] A few beginner problems From: Pascal Bourguignon - 2007-01-22 13:10 ```Hans3 writes: > Im a LISP beginner and I would like some help. > I have 2 problems that might be easy for an advanced programmer: Since this is not specific to clisp, but to Common Lisp, a better forum would be news:comp.lang.lisp =20 > 1. A function which will interpolate the values of 2 lists in a number = of > steps, so '(1 1 1 1 1) and '(0.5 1 1.5 2 3) would become: > ( 1.000 1.000 1.000 1.000 1.000 > 0.875 1.000 1.125 1.250 1.500 > 0.750 1.000 1.250 1.500 2.000 > 0.625 1.000 1.375 1.750 2.500 > 0.500 1.000 1.500 2.000 3.000 ) >=20 > How do I make this? Good question. How do YOU do this? =20 You've got three parameters, a from list, a to list, and a number of steps. So you should write a function: (defun interpolate (from-list to-list number-of-steps) ...) ; fill in the ellipsis. What did you do to get the above example result?=20 (or, if given by your teacher, how would YOU do it with:=20 (interpolate '(0 0 0 0) '(1 2 3 4) 6) ?) One hint: it would be easier/more natural to generate interpolations "line by line": (interpolate '(0 0) '(3 6) 4) --> ((0 0) (1 2) (2 4) (3 6)) If you want the result under the form (0 0 1 2 2 4 3 6), you can always concatenate the resulting sublists in a final step. What we have here is a division of the problem in two easier parts. It's easy ton concatenate lists, and it's easy (or at least easier than the original problem) to generate the interpolations "line by line"). > 2. A function that returns a list containing integers between sets of l= ower > and upper boundaries. For example: function 20 26 23 28 would return: > (20 21 22 23 24 25 26 27 28) > Any even number should be possible. Basically, the answer is the same;. How would YOU do it? How can you divide the problem in simplier subproblems? > 3. A random generator that chooses values form a list or a stackpile wi= thout > repeating any values until all have been used once. Idem. Do you really need to write a random generator (it's hard to do in computers). You could write a pseudo-random generator, but is it what's asked? (There is already a pseudo random generator function in Common Lisp). Perhaps what's asked is only to return the values from the stackpile in a random order? What we have here, is that you'd better know your programming language (and libraries), to be able to use existing operators to your purposes. (And also, becareful with specifications, they are often not precise enough and misleading). =20 > 4. A random generator that chooses values form a list or a stackpile wi= th a > specific weight. Example: (weight '(1 2 3) '(4 2 1)) means that 1 has a > weight of 4, 2 has a weight of 2 and 3 has a weight of 1, so 1 will be = twice > as much in a list that 2 and so on. Idem. =20 Hint: if you have a (pseudo) random generator giving integers between 0 and N-1 inclusive in equiprobability, how do you map these integers to a set of N elements to keep the equiprobability? And would you map these N integers to another set of elements to get different probabilities? > I would grately appreciate if anyone could tell me how to do this, so I= can > figure out the rest I want to do. Please help me out! >=20 > Thanks, Hans --=20 __Pascal Bourguignon__ http://www.informatimago.com/ READ THIS BEFORE OPENING PACKAGE: According to certain suggested versions of the Grand Unified Theory, the primary particles constituting this product may decay to nothingness within the next four hundred million years. ```

 [clisp-list] ISO-2022-JP From: Klaus Ebbe Grue - 2007-01-23 14:50 ```Hi clisp-list@..., If I type (map 'list 'char-code (convert-string-from-bytes #(65 92 27 40 74 92 168) (make-encoding :charset "ISO-2022-JP" :line-terminator :unix :input-error-action #\Null :output-error-action #\Null))) then I get (65 92 165 0) The input #(65 92 27 40 74 92 168) consists of an ascii A (65), an ascii backslash (92), a shift to Japanese sequence (27 40 74), a yen sign (92) and a katakana glyph (168). The output (65 92 165 0) consists of an ascii A (65), an ascii backslash (92) a yen sign (165) and an invalid character (0). So the shift to Japanese sequence (27 40 74) seems to be recognized, but katakana (code 161-223) does not seem to be recognized. Any suggestions? Cheers, Klaus --- [grue@... grue]\$ clisp --version GNU CLISP 2.41 (2006-10-13) (built 3370084031) (memory 3370084648) Software: GNU C 3.2 20020903 (Red Hat Linux 8.0 3.2-7) gcc -g -O2 -W -Wswitch -Wcomment -Wpointer-arith -Wimplicit -Wreturn-type -Wmissing-declarations -Wno-sign-compare -O2 -fexpensive-optimizations -falign-functions=4 -DUNICODE -DDYNAMIC_FFI -I. -x none libcharset.a libavcall.a libcallback.a /usr/local/lib/libreadline.so -Wl,-rpath -Wl,/usr/local/lib -lncurses -ldl -L/usr/local/lib -lsigsegv -lc -L/usr/X11R6/lib SAFETY=0 HEAPCODES LINUX_NOEXEC_HEAPCODES GENERATIONAL_GC SPVW_BLOCKS SPVW_MIXED TRIVIALMAP_MEMORY libsigsegv 2.4 libreadline 5.1 Features: (READLINE REGEXP SYSCALLS I18N LOOP COMPILER CLOS MOP CLISP ANSI-CL COMMON-LISP LISP=CL INTERPRETER SOCKETS GENERIC-STREAMS LOGICAL-PATHNAMES SCREEN FFI GETTEXT UNICODE BASE-CHAR=CHARACTER PC386 UNIX) C Modules: (clisp i18n syscalls regexp readline) Installation directory: /usr/local/lib/clisp/ User language: ENGLISH Machine: I686 (I686) thor.yoa.dk [127.0.0.1] ```

 Re: [clisp-list] A few beginner problems From: Hans3 - 2007-01-26 22:11 ```Thanks for the advice Klaus & Pascal! Although Pascal's advice is clear, I'm aware of the steps I need to follow to make these functions, I know this. What I don't know is the code... (because I don't have much experience yet) For now I have another question: How can I define a function with unlimited arguments? Example: (defun test (a &optional b c d e f g h i j k etc.... ) I would like it so that it doesnt matter how many arguments are there, so that it can be processed with a loop function until all arguments are used. Also it must be possible that in the function I can say: setf list (list *all arguments*) What command then do I need for *all arguments* ? -- View this message in context: http://www.nabble.com/A-few-beginner-problems-tf3050986.html#a8659740 Sent from the clisp-list mailing list archive at Nabble.com. ```

 Re: [clisp-list] A few beginner problems From: Klaus Ebbe Grue - 2007-01-27 10:07 ```> How can I define a function with unlimited arguments? > (defun test (a &optional b c d e f g h i j k etc.... ) I think you should *not* ask clisp-list@.... I will send an answer directly to tomkorver@.... -Klaus ```

 Re: [clisp-list] A few beginner problems From: Pascal Bourguignon - 2007-01-27 12:48 ```Hans3 writes: > Thanks for the advice Klaus & Pascal! > Although Pascal's advice is clear, I'm aware of the steps I need to fol= low > to make these functions, I know this. > What I don't know is the code... (because I don't have much experience = yet) Well, the question is what does it mean to interpolate? Assume we're considering only one couple of points=20 (interpolate '(4) '(8) 3) we have the points of coordinates (0,4) and (1,8) and we want to find one (=3D3-2, since we count these two points) point on the line that pass thru these two points. To do that, you need to compute the equation of the line from the two points, and then you need to divide the horizontal distance between the two points by the number of interpolated points plus one, and then, you can compute the y coordinate. In this example, the equation of the line passing thru (0,4) and (1,8) is: y=3D4+4x (since 4=3D4+4*0 and 8=3D4=3D4*1) and since we need only one point in the middle, with x =3D (1-0)/2 =3D 0.= 5 we find y =3D 4+4*0.5 =3D 6 therefore the result is: '((4) (6) (8)) So, what you need to do, is to write a function that finds the parameters a and b of the line y=3Dax+b, that passes between two points of coordinates (0,y0) and (1,y1). Then you must compute the list of intermediate y's for the serie of n x's going from 0 to 1.=20 Once you can do it for one couple of points, ou can do it for any number of couples of points given in two lists. --=20 __Pascal Bourguignon__ http://www.informatimago.com/ READ THIS BEFORE OPENING PACKAGE: According to certain suggested versions of the Grand Unified Theory, the primary particles constituting this product may decay to nothingness within the next four hundred million years. ```

 Re: [clisp-list] A few beginner problems From: Devon Sean McCullough - 2007-01-27 18:01 ```Date: Sat, 27 Jan 2007 11:07:22 +0100 (CET) From: Klaus Ebbe Grue > How can I define a function with unlimited arguments? > (defun test (a &optional b c d e f g h i j k etc.... ) I think you should *not* ask clisp-list@.... I will send an answer directly to tomkorver@.... Google [Hyperspec arguments] gets http://www.lisp.org/HyperSpec/Body/glo_a.html argument n. 1. (of a function) an object which is offered as data to the function when it is called. 2. (of a format control) a format argument. which is not helpful, mere links to equally terse glossary definitions, i.e., function, object, format control, format argument. A nice low-effort cure would be to cross-reference the glossary, e.g., click the "argument" definition for a page of links to 3.4.1 Ordinary Lambda Lists and so on. Surely someone did this already? Peace --Devon /~\ \ / Health Care X not warfare / \ Dubya won the digital vote Kerry won the popular vote ```

 [clisp-list] new-clx pointer needed From: Klaus Ebbe Grue - 2007-02-16 09:11 ```Hi clist-list, Can anyone give me a pointer to a new-clx development site, a new-clx faq or something? Reason: I did > (display-resource-id-base (close-display (open-display "127.0.0.1"))) and got > Internal error: statement in file "clx.f", line 1838 has been > reached!! Please send the authors of the program a description how > you produced this error! By the way, if a new-clx expert reads this e-mail: How does one test whether or not a given display is open or closed? I am trying to program a function (safe-close-display d) which closes d if d is an open display and does nothing if d is no display or a closed display. Cheers, Klaus ```

 Re: [clisp-list] new-clx pointer needed From: Sam Steingold - 2007-02-18 01:56 ```> * Klaus Ebbe Grue [2007-02-16 10:11:22 +0100]: > > Can anyone give me a pointer to a new-clx development site, a new-clx > faq or something? sources is your doc (other than the general CLX documentation) > Reason: I did > >> (display-resource-id-base (close-display (open-display "127.0.0.1"))) > > and got > >> Internal error: statement in file "clx.f", line 1838 has been >> reached!! Please send the authors of the program a description how >> you produced this error! XLIB:DISPLAY-RESOURCE-ID-BASE is not implemented, see the sources. http://www.cygwin.com/acronyms/#PTC > By the way, if a new-clx expert reads this e-mail: How does one test > whether or not a given display is open or closed? I am trying to > program a function (safe-close-display d) which closes d if d is an > open display and does nothing if d is no display or a closed display. XLIB:CLOSED-DISPLAY-P -- Sam Steingold (http://sds.podval.org/) on Fedora Core release 6 (Zod) http://jihadwatch.org http://truepeace.org http://palestinefacts.org http://honestreporting.com http://memri.org http://dhimmi.com Don't use force -- get a bigger hammer. ```

 Re: [clisp-list] new-clx pointer needed From: Klaus Ebbe Grue - 2007-02-18 10:38 ```Dear Sam, >> Can anyone give me a pointer to a new-clx development site, a new-clx >> faq or something? > > sources is your doc (other than the general CLX documentation) Thanks - (pointer to a new-clx development site) = nil was what I needed to get on. modules/clx/new-clx/README answers all my questions. >> How does one test whether or not a given display is open or closed? > > XLIB:CLOSED-DISPLAY-P Thanks - Klaus ```