Jump to content

Recommended Posts

Posted

please help how to convert this list

(setq a'("1 2.33 6.45 8.46" "2 8.98 4.78 1.58" "3 4.51 7.12 2.35" "4 6.95 5.64 7.82"))

to this list

(setq a '((1 2.33 6.45 8.46)(2 8.98 4.78 1.58)(3 4.51 7.12 2.35)(4 6.95 5.64 7.82)))

Posted (edited)

You use this:

(mapcar '(lambda(x) (read (strcat "(" x ")"))) a)

Edited by SLW210
Code Tags
Posted

Another .

 

(defun 2->list (l)
 (if l (setq l (cons (read (strcat "(" (car l) ")")) (2->list (cdr l)))))
)

Posted (edited)

You are welcome motee-z.

@Tharwat :

Thanks for the alternative, but in this case I think using mapcar is easier to understand than recursive.

Edited by 7o7
Posted

@Tharwat :

Thanks for the alternative, but in this case I think using mapcar is easier to understand than recursive.

 

I do agree with you . :)

Posted
(defun 2->list (l)
 (if l (setq l (cons (read (strcat "(" (car l) ")")) (2->list (cdr l)))))
)

Note that there is no need for the setq expression to redefine the variable 'l' -

The list will be constructed and returned as a consequence of the cons expression being the last expression evaluated.

Posted
Note that there is no need for the setq expression to redefine the variable 'l' -

The list will be constructed and returned as a consequence of the cons expression being the last expression evaluated.

 

You are right , thank you Lee for taking the time to correct me .

Posted
You use this:

(mapcar '(lambda(x) (read (strcat "(" x ")"))) a)

7o7. i noted your idea as good tips, thanks :)

i didn't notice it was string list by OP, luckily gurus are always here

Tharwat alternative idea & Lee simple yet useful advise. Thank you guys

Posted
7o7. i noted your idea as good tips, thanks :)

i didn't notice it was string list by OP, luckily gurus are always here

Tharwat alternative idea & Lee simple yet useful advise. Thank you guys

 

You are guru too, my friend :)

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...