# lisp / special divide - help needed

## Recommended Posts

LISP...
any solution? i want to DIVIDE a seq into sublists -> when it's asc into a list, "rest"  into single-listed values - thanx a lot for some help ```;;; input
(divide* '(14 12 3 13 15 8 4 10 17 2 16 0 1 6 7 5 11 9))

;;; output
=> ((14) (12) (3 13 15) (8) (4 10 17) (2 16) (0 1 6 7) (5 11) (9))```

##### Share on other sites

For a generic solution, you could use some function split-if that expects a Boolean function as argument comparing two consecutive pairs, and always splits a sublist if the Boolean function returns True. I don't have such a Lisp function at hand right now, but of an algorithm idea here is a similar Python function, only that this expects a Boolean function of a single argument only. If you port this function to Lisp, you could then generalise it by checking the number of arguments the given Boolean function expects, and it apply it always to the corresponding consecutive number of elements of xs.

```def split_if(fun, xs):
"""
Splits `xs` (list) into sublists based on a test. Starts a new sublist
at every element for which Boolean function `fun` returns true.

Examples
--------

>>> split_if(lambda x: x % 3 == 0, [0, 1, 2, 3, 4, 5])
[[0, 1, 2], [3, 4, 5]]

>>> split_if(lambda x: x % 3 == 0, [1, 2, 3, 4, 5, 6, 7])
[[1, 2], [3, 4, 5], [6, 7]]

NOTE:
Internally, `split_if` skips the first element of `xs`. So, if `fun` is
stateful, it should initialise its state using that first element.
For an example, see implementation of `format_api.split_into_sections`.
"""
result_list = []
start = 0
# Collect new list up to x whenever fun(x) == true, but not before first element
for index, x in enumerate(xs[1:]):
if fun(x):
result_list.append(xs[start:index+1])
start = index+1
if start == 0:
return [xs]
result_list.append(xs[start:])
return result_list```

Here is another approach as well with a Lisp function from my toolbox expecting a list of positions where to split the list. You could find those positions by iterating over conservative pairs of your list elements with the function positions-if listed below.

OK, none of this is a ready made solution, but all the necessary pieces are there I need to head back to work now ....

Torsten

```(defun subseqs (sequence positions &optional end)
"Splits `sequence' into a list of subsequences split at `positions'. Each element in `positions' specifies a position at which a new sublist starts.

; (subseqs '(0 1 2 3 4 5 6) '(0 2 4))
=> ((0 1) (2 3) (4 5 6))

; (subseqs '(0 1 2 3 4 5 6) '(2 4 6) 5)
=> ((2 3) (4))

`positions' are implicitly sorted and positions beyond end are excluded.
"
(let* ((updated-pos (sort (if end
(remove-if #'(lambda (x) (>= x end))
positions)
positions)
#'<))
(full-pos (append updated-pos
(list (if end
end
(length sequence))))))
(mapcar #'(lambda (start end)
(subseq sequence start end))
(butlast full-pos)
(rest full-pos))
))

(defun positions-if (predicate sequence &key key (start 0))
"Like the Common Lisp function `position-if', but returns all positions in `sequence' that match `predicate'.

; (positions-if #'oddp '((1) (2) (3) (4)) :key #'car)
; => (0 2)"
(labels ((aux (predicate sequence &key key (start 0) accum)
(let ((pos (position-if predicate sequence :start start :key key)))
(if pos
(aux predicate sequence :start (1+ pos) :key key :accum (cons pos accum))
(reverse accum)))))
(aux predicate sequence :start start :key key)))

#|
;; comparison
(position-if #'oddp '((1) (2) (3) (4)) :key #'car)
|#

```

##### Share on other sites

good approaches, thank you!!

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account. ×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

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

×
×

• Lessons
{"ft_lang_label":"__label__en","ft_lang_prob":0.8432462,"math_prob":0.94794375,"size":556,"snap":"2022-40-2023-06","text_gpt3_token_len":193,"char_repetition_ratio":0.08695652,"word_repetition_ratio":0.0,"special_character_ratio":0.4172662,"punctuation_ratio":0.14285715,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9679081,"pos_list":[0,1,2,3,4,5,6],"im_url_duplicate_count":[null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-02-08T13:41:15Z\",\"WARC-Record-ID\":\"<urn:uuid:117db02d-98fc-4ac2-9e40-cd1eee88c5b6>\",\"Content-Length\":\"126865\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:908cff08-02b1-41fa-b519-c3afd1156d38>\",\"WARC-Concurrent-To\":\"<urn:uuid:2e6bd9e5-70dc-41e5-a1b5-19af99beef84>\",\"WARC-IP-Address\":\"78.46.176.124\",\"WARC-Target-URI\":\"https://opusmodus.com/forums/topic/1492-lisp-special-divide-help-needed/#comment-4694\",\"WARC-Payload-Digest\":\"sha1:FQLT7Z3HEQJAZLZMKCEU2HCXH4SQERGK\",\"WARC-Block-Digest\":\"sha1:IFNYFUVUQOXHTHAPJ7VV6NOMGPEEVMY4\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-06/CC-MAIN-2023-06_segments_1674764500813.58_warc_CC-MAIN-20230208123621-20230208153621-00276.warc.gz\"}"}