HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Insert-everywhere function

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
functioneverywhereinsert

Problem

This is HtDP Excercise 12.4.2:


Develop a function insert-everywhere. It consumes a symbol and a list
of words. The result is a list of words like its second argument, but
with the first argument inserted between all letters and at the
beginning and the end of all words of the second argument.

This is what I wrote:

#lang racket

;Data Definition:
;a symbol, list of symbols

;Contract:
;insert-everywhere: symbol, list of symbols -> list of list of symbols
;The function returns a list of list of symbols wherein the symbol is inserted at every position

;Example:
; (insert-everywhere 'a '(b c d)) -> '((a b c d) (b a c d) (b c a d) (b c d a))

;Definition:

(define (insert-everywhere sym los)
  (define lst-len (find-len los))
  (define (iter n)
    (cond ((= n (+ lst-len 1)) '())
          (else (cons (insert n sym los) (iter (+ n 1))))))
  (iter 0))
;The above function first finds the list length. 
;Then it itereratively inserts the symbol at every position one by one, including in the end.

;Data Definition:
;list of symbols

;Contract:
;find-len : list of symbols-> number
;find the length of the list

;Example:
;(find-len (list 'b 'c 'd)) -> 3

;Definition:

(define (find-len los)
  (cond ((null? los) 0)
        (else (+ 1 (find-len (cdr los))))))

;Data Definition:
;number , symbol, list of symbols

;Contract:
;insert : position, element, list of symbols-> list of symbols
;insert the element in the given position in the list

;Example:
;(insert 2 'a '(b c d)) -> '(b c a d)

;Definition:

(define (insert pos elm los)
  (cond ((= pos 0) (cons elm los))
        (else (cons (car los) (insert (- pos 1) elm (cdr los))))))


Here is the tests I conducted:

(insert-everywhere 'a '()) ; returns '((a))
(insert-everywhere 'a '(b)) ; returns '((a b) (b a))
(insert-everywhere 'a '(b c d)) ; returns '((a b c d) (b a c d) (b c a d) (b c d a))


It works, but I think this is not the way it was supposed to be solved. In their hint, they ask to use only recur

Solution

Check this Stack Overflow question out, which has a solution to the same problem. A simplified version would look like this

(define (insert-at pos elmt lst)
 (if (empty? lst) (list elmt)
 (if (= 1 pos)
  (cons elmt lst)
  (cons (first lst) 
        (insert-at (- pos 1) elmt (rest lst))))))

(define (insert-everywhere sym los)
  (map (lambda (i)
         (insert-at i sym los)
           )
       (range 1 (+ 2 (length los)))))


-
There is a function written for inserting at a particular position.

-
Then we have our iterative function that maps over the elements in the list.

Alternatively, there is the recursive solution found in the link above. It does this by using conditions and cons (which appends to lists together). It seams a bit clunky, but map really cleans it up.

Code Snippets

(define (insert-at pos elmt lst)
 (if (empty? lst) (list elmt)
 (if (= 1 pos)
  (cons elmt lst)
  (cons (first lst) 
        (insert-at (- pos 1) elmt (rest lst))))))

(define (insert-everywhere sym los)
  (map (lambda (i)
         (insert-at i sym los)
           )
       (range 1 (+ 2 (length los)))))

Context

StackExchange Code Review Q#11805, answer score: 2

Revisions (0)

No revisions yet.