Jump to content

List to Columns


Jonathan Handojo

Recommended Posts

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

Link to comment
Share on other sites

Found it. Thanks for that... I kinda thought it should be a recursive function, but I didn't know how to write one XD

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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:

  • Funny 1
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

  • Like 1
Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...