#### [SOLVED] Extracting values from a list

By KRC

I had already solved this problem, but in an unnecessarily convoluted way that seems too specific. Can this be done better?

From a list:

(8 "layer-name" 10 x1 20 y1 10 x2 20 y2 10 x3 20 y3)

8, 10 and 20 are the "key-words" for layer-name, x-coord and y-coord respectively. The actual values follow the "key-words".

Return values should be --> '(("layer-name" x1 y1 x2 y2) ("layer-name" x2 y2 x3 y3))

``````#lang racket

(require srfi/1)

(define test-list '("multiple-lines" 8 "layer-name" 10 30 20 30   10 40 20 40   10 50 20 50))

(define (get-values lst lst2)
(cond ((eq? lst2 '())
'())
(else
(cons (cadr (member (car lst2) lst))
(get-values lst (cdr lst2))))))

(define (get-multiple-values lst key key2) ; key is single values. key2 is repeatable values
(define (splitter lst keyword) ; (splitter '(10 x1 20 y1 10 x2 20 y2 10 x3 20 y3) 10 20)  --->  '((x1 y1 x2 y2 x3 y3) (x2 y2 x3 y3) (x3 y3))
(cond ((not (member keyword lst)) '())
(else (cons (member keyword lst) (splitter (cddr (member keyword lst)) keyword)))))
(define (joiner lst lst-of-lsts) ; (joiner '(1 2) '((a b) (c d)))  --->  '((1 2 a b) (1 2 c d))
(cond ((empty? lst-of-lsts) '())
(else (cons (append lst (car lst-of-lsts))
(joiner lst (rest lst-of-lsts))))))
(define (trimmer size lst-of-lsts) ; (trimmer  '((x1 y1 x2 y2 x3 y3) (x2 y2 x3 y3) (x3 y3)))
(cond ((empty? lst-of-lsts) '())
((> size (length (car lst-of-lsts)))
(trimmer size (rest lst-of-lsts)))
(else (cons (take (car lst-of-lsts) size)
(trimmer size (rest lst-of-lsts))))))
(joiner (get-values lst key)
(trimmer 4 (map
(lambda (lst) (remq* key2 lst))
(splitter lst (first key2))))))

> '(("layer-name" 30 30 40 40) ("layer-name" 40 40 50 50))
``````

#### @Óscar López 2014-12-11 16:20:53

I have a strong suspicion that this is an XY Problem, or at the very least, that you're using the wrong abstraction and/or data structure for representing data, using numbers as keywords somehow doesn't seem right. Anyway, I think the requested functionality can be implemented in an alternate, simpler way:

``````; obtain all values for a given keyword
(define (key-values lst kwd)
(cond ((null? lst) '())
((equal? (first lst) kwd)
(cons (second lst) (key-values (cddr lst) kwd)))
(else (key-values (cddr lst) kwd))))

; interleave elements from two lists
(define (interleave lst1 lst2)
(cond ((null? lst1) lst2)
((null? lst2) lst1)
(else (cons (first lst1) (interleave lst2 (rest lst1))))))

; create all layers according to the defined structure
(define (joiner name lst1 lst2)
(let loop ((lst (interleave lst1 lst2)))
(if (null? (cddr lst))
'()
(cons (list name (first lst) (second lst) (third lst) (fourth lst))
(loop (cddr lst))))))

; main function, receives a list of values and a list of keywords
(define (get-values lst kwds)
(let ((name (first (key-values lst (first kwds))))
(xs   (key-values lst (second kwds)))
(ys   (key-values lst (third kwds))))
(joiner name xs ys)))
``````

For example:

``````(define test-list '(8 "layer-name" 10 30 20 30 10 40 20 40 10 50 20 50))
(define keywords '(8 10 20))
(get-values test-list keywords)
=> '(("layer-name" 30 30 40 40) ("layer-name" 40 40 50 50))
``````

#### @KRC 2014-12-12 00:24:23

Is it really a XY problem? TO give more context, i am reading DXF files. The output stream from the DXF file is a list of strings, and the format is a keyword-number anywhere from 0 to 329, which specifies the type of the following value. Perhaps i should create a general function that reads the entire list of strings and converts the values into their proper type from a string depending on the keyword?

#### @Óscar López 2014-12-12 02:09:20

@krc that might be a good idea, I'm not familiar with that file format, but I'd suggest that first you look for an existing library to do the job, or perhaps a generic parser for reading streams with a given format. Anyway, the question in the comment doesn't resemble the original question, so it's up to you if you deem my answer useful. You should have explained the real situation from the beginning, this was indeed and XY problem.

#### @ben rudgers 2014-12-29 19:16:21

@ÓscarLópez Some Context: DXF is the file format used by AutoCad. It is a text file representation of a more compact binary format, DWG. People have been using Lisps to manipulate it since 1986 when AutoLisp was introduced. The rough analogy I like is that AutoCad is Emacs for vectors and by extension carries similar historical baggage. Anyway, the question is a bit of an iceberg and there's a meaningful mass under the surface.

### [SOLVED] Remove first occurrence of element from list

• 2013-10-29 22:17:15
• ricardo
• 2494 View
• 2 Score
• Tags:   scheme

### [SOLVED] A list builder or list accumulator

• 2011-08-27 08:56:10
• Shannon Severance
• 840 View
• 3 Score
• Tags:   scheme

### [SOLVED] Function that tests whether all items in a list are the same

• 2016-10-17 05:21:10
• misha
• 103 View
• 1 Score
• Tags:   scheme

### [SOLVED] Remove the last occurrence of an item in a list

• 2016-10-16 14:56:16
• misha
• 3167 View
• 2 Score
• Tags:   scheme

### [SOLVED] Splitting a list in Racket

• 2015-04-16 07:46:30
• Stefano Sanfilippo
• 9487 View
• 4 Score
• Tags:   scheme racket

### [SOLVED] Merging list tails in Scheme

• 2015-10-21 08:36:24
• Chris Jester-Young
• 61 View
• 4 Score
• Tags:   scheme

### [SOLVED] Sum of squares of 2 largest numbers out of a list of 3 numbers

• 2015-02-01 13:28:16
• guest
• 1347 View
• 4 Score
• Tags:   scheme