NagyMusic Posted February 7, 2022 Share Posted February 7, 2022 I wondered if someone would be willing to help me with a snippet of code. I'm trying to generate a series of 100 sublist 2 digit-pairs. The first of those digit-pairs randomly (or according to a predefined vector) alternates between 1 and 2 only: '((1 #) (2 #) (2 #) (1 #) (2 #) (1 #) (1 #) etc). The second of those digit-pairs unfolds numbers from 1 to 100 between every consecutive sub-list pair: '((# 1) (# 2) (# 3) (# 4) (# 5) etc). The result would be something like this: '((1 1) (2 2) (2 3) (1 4) (2 5) (1 6) (1 7) ... (2 100)) Thank you! Quote Link to comment Share on other sites More sharing options...
Stephane Boussuge Posted February 7, 2022 Share Posted February 7, 2022 Hi Zvony, here's a possible solution: (setf list1 (rnd-number 100 1 2)) (setf list2 (gen-integer 1 100)) (setf interleaved-list (matrix-transpose (list list1 list2))) S. NagyMusic 1 Quote Link to comment Share on other sites More sharing options...
AM Posted February 7, 2022 Share Posted February 7, 2022 here is a pure LISP/CCL solution (loop for i from 1 to 100 collect (list (1+ (random 2)) i)) => ((1 1) (2 2) (2 3) (2 4) (1 5) (1 6) (1 7) (2 8) (1 9) (1 10) (1 11) (1 12) (2 13) (2 14) (1 15) (2 16) (2 17) (2 18) (2 19) (2 20) (1 21) (2 22) (2 23) (1 24) (2 25) (2 26) (1 27) (2 28) (2 29) (2 30) (2 31) (2 32) (2 33) (1 34) (2 35) (2 36) (1 37) (1 38) (2 39) (1 40) (1 41) (1 42) (1 43) (2 44) (2 45) (2 46) (2 47) (1 48) (2 49) (1 50) (2 51) (1 52) (2 53) (1 54) (1 55) (1 56) (2 57) (2 58) (1 59) (2 60) (1 61) (1 62) (1 63) (2 64) (1 65) (1 66) (2 67) (1 68) (1 69) (2 70) (2 71) (2 72) (1 73) (1 74) (2 75) (1 76) (1 77) (2 78) (2 79) (1 80) (2 81) (2 82) (1 83) (2 84) (2 85) (1 86) (1 87) (2 88) (2 89) (1 90) (2 91) (1 92) (1 93) (1 94) (2 95) (2 96) (2 97) (1 98) (1 99) (2 100)) as a function (defun pairs (n) (loop for i from 1 to n collect (list (1+ (random 2)) i))) (pairs 200) Quote Link to comment Share on other sites More sharing options...
NagyMusic Posted February 7, 2022 Author Share Posted February 7, 2022 Thank you both! This is very helpful and informative - I really appreciate! Quote Link to comment Share on other sites More sharing options...
Stephane Boussuge Posted February 8, 2022 Share Posted February 8, 2022 My solution with loop: (loop for n1 in (rnd-number 100 1 2) for n2 in (gen-integer 1 100) collect (list n1 n2)) S Quote Link to comment Share on other sites More sharing options...
opmo Posted February 8, 2022 Share Posted February 8, 2022 (mclist '(0 1 2 3) '(a b c d)) => ((0 a) (1 b) (2 c) (3 d)) NagyMusic and Stephane Boussuge 1 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.