## clisp-list

 [clisp-list] A few beginner problems From: Hans3 - 2007-01-21 23:25:34 Hi, Im a LISP beginner and I would like some help. I have 2 problems that might be easy for an advanced programmer: 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 ) How do I make this? 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) Any even number should be possible. 3. A random generator that chooses values form a list or a stackpile without repeating any values until all have been used once. 4. A random generator that chooses values form a list or a stackpile with 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. 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! Thanks, Hans -- View this message in context: http://www.nabble.com/A-few-beginner-problems-tf3050986.html#a8481115 Sent from the clisp-list mailing list archive at Nabble.com.
 Re: [clisp-list] A few beginner problems From: Klaus Ebbe Grue - 2007-01-22 09:13:21 ;; 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
 [clisp-list] ISO-2022-JP From: Klaus Ebbe Grue - 2007-01-23 14:50:57 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]