Jonathan Handojo Posted April 28, 2020 Share Posted April 28, 2020 Hi guys, Who has the shortest way to do something like the reverse of (apply 'append assoc_list)? I'm simply looking for a function that can sort one long list into an association list of x items each. So for example, if your function is (list-to-columns lst cols), then calling: (list-to-columns '(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14) 4) will return ((0 1 2 3) (4 5 6 7) (8 9 10 11) (12 13 14)) Thanks, Jonathan Handojo Quote Link to comment Share on other sites More sharing options...
Grrr Posted April 28, 2020 Share Posted April 28, 2020 Look for a GroupByN subfoo somewhere here. Quote Link to comment Share on other sites More sharing options...
Jonathan Handojo Posted April 28, 2020 Author Share Posted April 28, 2020 Found it. Thanks for that... I kinda thought it should be a recursive function, but I didn't know how to write one XD Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted April 28, 2020 Share Posted April 28, 2020 Here's my take on it: Group List by Number Quote Link to comment Share on other sites More sharing options...
Jonathan Handojo Posted April 28, 2020 Author Share Posted April 28, 2020 Didn't know that existed in your website. I didn't know what it was called so... When I actually wrote an iterative approach before posting this question (thinking that it was a non-efficient function), it was actually more or less the same as your iterative approach... which is why I hope there was a recursive approach somewhere (because I'm weak in recursive functions, but willing to find examples to learn from). Thanks Lee. Quote Link to comment Share on other sites More sharing options...
Grrr Posted April 29, 2020 Share Posted April 29, 2020 1 hour ago, Jonathan Handojo said: which is why I hope there was a recursive approach somewhere (because I'm weak in recursive functions, but willing to find examples to learn from) Back in the time was playin' around with such - (setq pL '(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15)) ('((f L)(f L)) '(( L ) (if L (cons (mapcar ''( (x) (nth x L)) '(0 1 2)) (f (cdddr L))))) pL) Now I'm so tight that I can't take the time to write out a decent reply to my own threads :sad: 1 Quote Link to comment Share on other sites More sharing options...
Jonathan Handojo Posted April 29, 2020 Author Share Posted April 29, 2020 This may seem off the topic, but I'm pretty sure it's online somewhere, I just can't find it... Do you also happen to know of a function that can list all possible combinations of a list if you're supplied an association list? Quote Link to comment Share on other sites More sharing options...
Jonathan Handojo Posted April 29, 2020 Author Share Posted April 29, 2020 Never mind, I figured it out... I went through all sorts of hell to figure it out, lost half my hair (defun JH:listcombination (lst) (if (null lst) '(nil) (apply 'append (mapcar '(lambda (x) (mapcar '(lambda (y) (cons x y) ) (JH:listcombination (cdr lst)) ) ) (car lst) ) ) ) ) I was simply toying around, though I'm not sure how it actually ended up with the desired result. 1 Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted April 29, 2020 Share Posted April 29, 2020 (edited) Some others: https://www.theswamp.org/index.php?topic=2694 https://www.theswamp.org/index.php?topic=47435.msg524472#msg524472 https://www.theswamp.org/index.php?topic=30434.msg360831#msg360831 http://lee-mac.com/permutations.html Edited April 29, 2020 by Lee Mac 1 Quote Link to comment Share on other sites More sharing options...
hanhphuc Posted May 1, 2020 Share Posted May 1, 2020 On 4/29/2020 at 11:52 AM, Jonathan Handojo said: (defun JH:listcombination (lst) I was simply toying around, though I'm not sure how it actually ended up with the desired result. nice! you can trace your function in the console 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.
Note: Your post will require moderator approval before it will be visible.