Skip to content

Instantly share code, notes, and snippets.

@jasper-lyons
Created December 6, 2017 17:52
Show Gist options
  • Save jasper-lyons/2a7f9e77255c4f2551844f55b831c004 to your computer and use it in GitHub Desktop.
Save jasper-lyons/2a7f9e77255c4f2551844f55b831c004 to your computer and use it in GitHub Desktop.
#! /usr/bin/env guile -s !#
; guile needs the bash script comment closed
; saves an anonymous lambda later
(define inc (lambda (x) (+ x 1)))
; guiles list-set! function updates a list inplace but I want it to be immutable
; so I can use the old value from the list
(define (update-list lst index func)
(update-list-rec lst index func 0))
; classic recursive func definition, should probably do it inside of the
; api func definition
(define (update-list-rec lst index func count)
(cond
((eq? '() lst) lst)
((= index count)
(cons (func (car lst)) (cdr lst)))
((> index count)
(cons
(car lst)
(update-list-rec
(cdr lst)
index
func
(+ count 1))))))
(define (jump-list lst)
(jump-list-rec lst 0 0))
(define (jump-list-rec lst index count)
(cond
((eq? '() lst) -1)
((> index (- (length lst) 1)) count)
(else (jump-list-rec
(update-list lst index inc)
(+ index (list-ref lst index))
(+ count 1)))))
; ommit a tone of numbers because...
(define program (list 0
2
0
...
-34))
(display (jump-list program))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment