libera/#commonlisp - IRC Chatlog
Search
14:17:01
beach
What standard special forms or macros have a list of "bindings" other than LET, LET*, HANDLER-BIND, and RESTART-BIND?
14:18:35
beach
I mean syntactically, so that some part of the form can be identified as such a list of bindings.
14:22:44
ogamita
Also, binding is something that is established at run-time, so I wouldn't make such a difference between progv and let…
14:23:20
beach
For context, I am working on Iconoclast, which is a library defining ASTs for standard special forms and macros, and I am trying to identify reasonable mixin classes. That's why I am interested only in operators where the list of bindings is syntactically apparent.
14:25:34
bike
the format of the bindings is different for a lot of these, though. like LET is symbol | (symbol [value]), which is not what handler-bind or flet have.
14:38:29
beach
The idea is to extract certain features to mixin classes to avoid code duplication. Like bindings, declarations, documentation, body, clauses, ...
20:11:07
HamzaShahid
I was solving another projecteuler problem and somehow managed to make it a binary addition problem
20:11:38
HamzaShahid
All I want now is to have a list of lists containing each digit of a binary number going from 000 to 111
20:13:43
HamzaShahid
but it should be generated dynamically be saying how many digits of binary to calculate from. Any idea on how to add and operate on raw binary in common lisp. Any help would be greatly appreciated
20:15:06
HamzaShahid
PS. I know there are libraries like cl-binary but I want to do it with raw common lisp for educational purposes
20:26:16
pjb
HamzaShahid: (defun all-bit-combinations (n) (if (zerop n) '(()) (let ((rests (all-bit-combinations (- n 1)))) (mapcan (lambda (bit) (mapcar (lambda (rest) (cons bit rest)) rests)) '(0 1))))) (mapcar #'all-bit-combinations '(0 1 2 3)) #| --> ((nil) ((0) (1)) ((0 0) (0 1) (1 0) (1 1)) ((0 0 0) (0 0 1) (0 1 0) (0 1 1) (1 0 0) (1 0 1) (1 1 0) (1 1 1))) |#
20:28:32
pjb
HamzaShahid: this is one way. But another is: (defun all-bit-combinations (n) (loop for word from 0 below (expt 2 n) collect (integer-to-list-of-bits word n))) with (defun integer-to-list-of-bits (n width) (loop for i below width collect (if (logbitp i n) 1 0))) (all-bit-combinations 3) #| --> ((0 0 0) (1 0 0) (0 1 0) (1 1 0) (0 0 1) (1 0 1) (0 1 1) (1 1 1)) |#
20:31:15
pjb
nij-: yes, but you're advised not to change the parameters on which the dispatching has been done.
20:31:48
pjb
if your generic function has &key &rest or &optional, you may add parameters to call-next-method (pass the mandatory ones first).
20:32:49
pjb
HamzaShahid: also, why use list of bits? You could use bit-vectors instead. (defun integer-to-list-of-bits (n width) (loop with result = (make-array width :element-type 'bit :initial-element 0) for i below width when (logbitp i n) do (setf (aref result i) 1) finally (return result))) (all-bit-combinations 3) #| --> (#*000 #*100 #*010 #*110 #*001 #*101 #*011 #*111) |#
20:33:41
pjb
HamzaShahid: perhaps you could even use 2D arrays of bits? (make-array '(8 3) :element-type 'bit :initial-contents (all-bit-combinations 3)) #| --> #2A(#*000 #*100 #*010 #*110 #*001 #*101 #*011 #*111) |# ?
20:38:11
HamzaShahid
Ooooh cool thanks everyone, so turns out my solution is the worst but I will still present it :)
20:49:32
HamzaShahid
Used this at the end: (loop :for i :below (expt 2 (1- rows)) :collect (loop :for j :below 3 :collect (if (logbitp j i) 1 0)))
21:04:30
pjb
HamzaShahid: if you want to go thru a string you can do (defun integer-to-list-of-bits (n width) (map 'list (lambda (ch) (if (char= #\1 ch) 1 0)) (format nil "~VB" width n))) (integer-to-list-of-bits 9 5) #| --> (0 1 0 0 1) |#
21:08:07
pjb
HamzaShahid: or, using functions like you: (map 'bit-vector (compose parse-integer string) (format nil "~V,'0B" 5 9)) #| --> #*01001 |#
21:10:18
pjb
I've used the macro com.informatimago.common-lisp.cesarum.utility:compose, but you may use the function (alexandria:compose #'parse-integer #'string).
21:56:19
nij-
In a function I'd like to check if a statement is true or not, but I don't want to do it always, as it will slow down the system.
22:02:56
pjb
nothing implementation independent let you do that. You have to make your own flag and test.
22:03:16
pjb
nij-: in general, the question is whether you want this test to be made at compilation-time or at run-time.
22:04:21
pjb
nij-: at compilation-time you may use a feature and #+with-assertions (assert expression) Then you can write: (progn (declaim (optimization (safety 3))) (pushnew :with-assertions *features*)) before compiling.
22:04:54
pjb
nij-: at run-time: (defvar *with-assertions* nil) (when *with-assertions* (assert expression)) Then at run-time you may (setf *with-assertions* t).
22:14:41
bike
note that safety is a syntactic property, so it'll just be the safety at compile time, not at run time
22:14:53
pjb
You could do: (eval-when (:compile-toplevel :load-toplevel :execute) (defvar *optimizations* '()) (setf *optimizations* '(optimization (safety 3))) (proclaim *optimizations*))) to save the current optimization. But you'd have to remember doing that everytime, everywhere.
22:44:15
bike
it's an earlier version of common lisp that had some operator for interrogating environments, which some implementations support as an extension
22:45:09
bike
https://github.com/Zulu-Inuoe/trivial-cltl2 https://github.com/alex-gutev/cl-environments declaration-information can be used to see what safety is