|
General Details
|
|
Scheme
|
|
Posted 76 Days Ago
|
|
94 Views
|
|
Received 1 Rating
|
|
Splitting a list recursively in Scheme
Description
If you're like me, you've been programming in Scheme and wanted to split a list into sub-lists of [semi]-equal length. I'm sure there's a library function to do this (I didn't even look), but I wanted to do it recursively. So here's how it should work:
(split 2 '(1 2 3 4))
=> ((1 2) (3 4))
(split 3 '(1 2 3 4))
=> ((1) (2) (3 4))
(split 3 '(1 2 3 4 5))
=> ((1) (2 3) (4 5))
As you can see, if the list can't be split evenly the function will create lists of as equal length as possible. I chose to take the shorter lists first.
Technical
I use Guile when testing, but I don't think it should matter.
Source Code
Comments
| Please login to post comments. |
|
|
I'm happy I met someone who does Scheme :). Nice little app. You should note that the standard for closing parenthesis is something like this:
(fcn (fcn2 (fcn3 1 3 5)))
That is, do not line them up like you would with other parenthesis in something like Java.
|
More "Scheme" Source Codes By This Author
Recently Posted "Scheme" Source Codes
Recently Rated "Scheme" Source Codes
|