81711

Inserting at arbitrary position in list in Scheme

Question:

I have a list with me, for example: (B D F)

I want to insert an element at an arbitrary position in the list. For example, if the element is A, I want to insert it before B and if the element C, I want to insert it after B but before D.

Is there any way to insert elements at an arbitrary position in a list in Scheme?

Answer1:

It's easy to implement a function for this:

(define (insert-at new k lst) (cond ((null? lst) (list new)) ((zero? k) (cons new lst)) (else (cons (car lst) (insert-at new (sub1 k) (cdr lst))))))

For example:

(insert-at 'B 1 '(A)) => '(A B) (insert-at 'A 0 '(B D F)) => '(A B D F) (insert-at 'C 2 '(A B D F)) => '(A B C D F)

Recommend

  • Enabling or disabling validation upon context
  • Python How to force object instantiation via Context Manager?
  • Monotouch - Issue with QLPreviewController
  • Set cookie from Web Api 2 IAuthenticationFilter AuthenticateAsync method
  • Problem glTexGen in Open GL ES 2.0
  • Use of qualified name in function parameter
  • PHP: Very simple Encode/Decode string
  • Calling C function from lua
  • Android changing fragment order inside FragmentPagerAdapter
  • Jquery popup on mouse over of calendar control
  • C: Incompatible pointer type initializing
  • Django simple Captcha “No module named fields” error
  • why xml file does not aligned properly after append the string in beginning and end of the file usin
  • GridView breaks while scrolling
  • Blackberry - Custom EditField Cursor
  • How do I get HTML corresponding to current DOM tree?
  • How to create a file in java without a extension
  • Body moving without any force applied? (Box2d)
  • PHP - How to update data to MySQL when click a radio button
  • C# - Serializing and deserializing static member
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Bug in WPF DataGrid
  • Why doesn't :active or :focus work on text links in webkit? (safari & chrome)
  • Incrementing object id automatically JS constructor (static method and variable)
  • Join two tables and save into third-sql
  • How to handle AllServersUnavailable Exception
  • When should I choose bucket sort over other sorting algorithms?
  • Weird JavaScript statement, what does it mean?
  • Unanticipated behavior
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Android Google Maps API OnLocationChanged only called once
  • Turn off referential integrity in Derby? is it possible?
  • Add sale price programmatically to product variations
  • Django query for large number of relationships
  • Binding checkboxes to object values in AngularJs
  • Unable to use reactive element in my shiny app
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?
  • How do I use LINQ to get all the Items that have a particular SubItem?