Pages

Popular Posts

Sunday, April 26, 2020

Scheme Programming







Introduction

The scheme is a minimalist dialect of the Lisp family of programming languages. The scheme consists of a small standard core with powerful tools for language extension. more...

Example Questions.

Create a file called a6q1_lists.scm and place your answers to the following questions in this file. Please note: failure to use the correct filename, function name, or function signature will result in a mark of zero for the effected solutions.
  1. Write a Scheme function called join that takes a key and a nested list as arguments and returns a list with all the elements from the top level of the input list joined together with the key between each.
    E.g., (join 'and '((1) (2) (3) (4))) → (1 and 2 and 3 and 4)
    E.g., (join 'x '((a b) ((c d) e) (f (g) h))) → '(a b x (c d) e x f (g) h)
  2. Write a Scheme function called split that takes a key and a list as arguments and splits the input list into a list of sublists between each instance of key.
    E.g., (split 'c '(a b c d e)) → ((a b)(d e))
    E.g., (split '0 '(1 0 1 1 0 2 0 0 1)) → ((1)(1 1)(2)(1))
  3. Write a Scheme function called slice that takes a list and two integer indices as arguments and returns a sublist of the input list between the indices start (inclusive) and end (exclusive). If the given indices are out of bounds, then the returned sublist should include any existing values in the given ranges.
    E.g., (slice '(0 1 2 3 4 5 6 7 8 9) 3 8) → (3 4 5 6 7)
    E.g., (slice '(0 1 2 3 4 5 6 7 8 9) 5 25) → (5 6 7 8 9)
    E.g., (slice '(0 1 2 3 4 5) -10 3) → (0 1 2)
  4. Write a Scheme function called splice that takes the following as arguments: an input list, an index (integer), a count (integer), and a list of new items. The splice function should remove count items from the input list at the given index, and insert all of the new items at that location.
    E.g., (splice '(a b c d e) 2 1 '(x y)) → (a b x y d e)
    E.g., (splice '(a b c d e) 2 0 '(x y)) → (a b x y c d e)
    E.g., (splice '(a b c d e) 2 1 '()) → (a b d e)

Answers

;; `a` Sub Question
(define indexOfJoinList '0)
(define joinTempLst '())
(define joinOutLst '())
(define (join key lst)
  (for-each
    (lambda (i)
      (cond ((< (+ indexOfJoinList 1) (length lst))
              (set! joinTempLst (cons i joinTempLst))
              (set! joinTempLst (cons key joinTempLst))
            )
        (else
          (set! joinTempLst (cons i joinTempLst))
        ))
      (set! indexOfJoinList (+ indexOfJoinList 1))
    ) lst)

  ;  reverse the list
  (for-each
    (lambda (i)
      (set! joinOutLst (cons i joinOutLst))
    ) joinTempLst)

  (write joinOutLst)
  ; reset the list
  (set! joinOutLst '())
  (set! joinTempLst '())
  (set! indexOfJoinList 0)
)


;; `b` Sub Question
(define indexOfSplitList '0)
(define splitTempList '())
(define splitOutLst '())
;; temp item list
(define splitItemList '())
(define splitItemListReverse `())

(define (split key lst)
  (for-each
    (lambda (i)
      (cond ((equal? i key)
              ;  reverse the list
              (for-each
                (lambda (k)
                  (set! splitItemListReverse (cons k splitItemListReverse))
                ) splitItemList)
              ; add to the temp list
              (cond ((> (length splitItemListReverse) 0)
                      (set! splitTempList (cons splitItemListReverse splitTempList))
                    ) (else ()))
              ;; reset list
              (set! splitItemListReverse '())
              (set! splitItemList '())
            )
        (else
          (set! splitItemList (cons i splitItemList))
        ))
      (set! indexOfSplitList (+ indexOfSplitList 1))
    ) lst)

  ;  reverse the list
  (for-each
    (lambda (k)
      (set! splitItemListReverse (cons k splitItemListReverse))
    ) splitItemList)
  ; add to the temp list

  (set! splitTempList (cons splitItemListReverse splitTempList))
  ;; reset list
  (set! splitItemListReverse '())
  (set! splitItemList '())

  (cond ((> (length splitItemListReverse) 0)
          (set! splitTempList (cons splitItemListReverse splitTempList))
        ) (else ()))
  ;  reverse the list
  (for-each
    (lambda (i)
      (set! splitOutLst (cons i splitOutLst))
    ) splitTempList)


  (write splitOutLst)
  ; reset the list
  (set! splitOutLst '())
  (set! splitTempList '())
  (set! indexOfSplitList 0)
)

;; `c` Sub Question
(define indexOfSliceList '0)
(define sliceTempLst '())
(define sliceOutLst '())
(define (slice lst first last)

  (for-each
    (lambda (i)
      (cond ((and (<= first indexOfSliceList) (> last indexOfSliceList))
              (set! sliceTempLst (cons i sliceTempLst))
            ) (else ()))
      (set! indexOfSliceList (+ indexOfSliceList 1))
    ) lst)

  ;  reverse the list
  (for-each
    (lambda (i)
      (set! sliceOutLst (cons i sliceOutLst))
    ) sliceTempLst)

  (write sliceOutLst)

  ; reset the list
  (set! sliceOutLst '())
  (set! sliceTempLst '())
  (set! indexOfSliceList 0)
)


;; `d` Sub Question
(define indexOfSpliceList '0)
(define spliceTempLst '())
(define spliceOutLst '())
(define endFlag 0)
(define count 0)
(define (splice lst first last lst2)
  (set! count 0)
  ; reset the list
  (set! spliceOutLst '())
  (set! spliceTempLst '())
  (set! indexOfSpliceList 0)
  (set! endFlag 0)
  (for-each
    (lambda (i)
      (cond ((<= first indexOfSpliceList)
              (cond ((equal? endFlag 0)
                      (for-each
                        (lambda (k)
                          (set! spliceTempLst (cons k spliceTempLst))
                        ) lst2)
                      (set! endFlag 1)
                    ) (else ()))

              (cond ((< count last)
                      (set! count (+ count 1))
                    ) (else (set! spliceTempLst (cons i spliceTempLst))))

            ) (else (set! spliceTempLst (cons i spliceTempLst))))
      (set! indexOfSpliceList (+ indexOfSpliceList 1))
    ) lst)
  ;  reverse the list
  (for-each
    (lambda (i)
      (set! spliceOutLst (cons i spliceOutLst))
    ) spliceTempLst)
  (write spliceOutLst)
)

(newline)
(display " ======================= Q1 a  =============================")
(newline)

(display " (join 'and '((1) (2) (3) (4))) => ")(newline)
(display "Expected: (1 x 2 x 3 x 4)")(newline)
(display "Actual: ")(join 'x '((1) (2) (3) (4)))(newline)

(newline)

(display " (join 'x '((a b) ((c d) e) (f (g) h))) => ")(newline)
(display "Expected: (a b x (c d) e x f (g) h)")(newline)
(display "Actual: ")(join 'x '((a b) ((c d) e) (f (g) h)))(newline)



(newline)
(display " ======================= Q1 b  =============================")
(newline)


(display " (split 'c '(a b c d e)) => ")(newline)
(display "Expected: ((a b)(d e))")(newline)
(display "Actual: ")(split 'c '(a b c d e))(newline)

(newline)

(display " (split '0 '(1 0 1 1 0 2 0 0 1))  => ")(newline)
(display "Expected: ((a b)(d e))")(newline)
(display "Actual: ")(split '0 '(1 0 1 1 0 2 0 0 1))(newline)



(newline)
(display " ======================= Q1 c =============================")
(newline)

(display " (slice '(0 1 2 3 4 5 6 7 8 9) 3 8) => ")(newline)
(display "Expected: (3 4 5 6 7)")(newline)
(display "Actual: ")(slice '(0 1 2 3 4 5 6 7 8 9) 3 8)(newline)

(newline)

(display " (slice '(0 1 2 3 4 5 6 7 8 9) 5 25)  => ")(newline)
(display "Expected: (5 6 7 8 9)")(newline)
(display "Actual: ")(slice '(0 1 2 3 4 5 6 7 8 9) 5 25)(newline)

(newline)

(display " (slice '(0 1 2 3 4 5) -10 3) => ")(newline)
(display "Expected: (0 1 2)")(newline)
(display "Actual: ")(slice '(0 1 2 3 4 5) -10 3)(newline)

(newline)
(display " ======================= Q1 d  =============================")
(newline)

(display " (splice '(a b c d e) 2 1 '(x y)) => ")(newline)
(display "Expected: (a b x y d e)")(newline)
(display "Actual: ")(splice '(a b c d e) 2 1 '(x y))(newline)

(newline)

(display " (splice '(a b c d e) 2 0 '(x y))  => ")(newline)
(display "Expected: (a b x y c d e)")(newline)
(display "Actual: ")(splice '(a b c d e) 2 0 '(x y))(newline)

(newline)

(display " (splice '(a b c d e) 2 1 '()) => ")(newline)
(display "Expected: (a b d e)")(newline)
(display "Actual: ")(splice '(a b c d e) 2 1 '())(newline)