PasteRack.org
Paste # 11668
2014-10-30 10:01:04

Fork as a new paste.

Paste viewed 69 times.


Embed:

  1. #lang racket
  2.  
  3. (define width 8)
  4.  
  5. (define (left-shift ls)
  6.   (define (shift result ls)
  7.     (if (>= 1 (length ls))
  8.         (append ls result)
  9.         (let ([a (first ls)]
  10.               [b (second ls)])
  11.           (if (= a b)
  12.               (shift result (cons (* 2 a) (drop ls 2)))
  13.               (shift (cons a result) (drop ls 1))))))
  14.   (let* ([result (shift '() (filter (compose not zero?) ls))]
  15.          [w (- width (length result))])
  16.     (append (reverse result) (make-list w 0))))
  17.  
  18. (left-shift '(1 1 2 0 4 8))

=>

'(16 0 0 0 0 0 0 0)