Skip to content

Instantly share code, notes, and snippets.

@ddeaguiar
Created December 31, 2013 02:28
Show Gist options
  • Save ddeaguiar/8191566 to your computer and use it in GitHub Desktop.
Save ddeaguiar/8191566 to your computer and use it in GitHub Desktop.
#lang racket
;; Named let (Section '4.6.4 Named let' of
;; http://docs.racket-lang.org/guide/let.html)
(define (duplicate-shorthand pos lst)
(let dup ([i 0]
[lst lst])
(cond
[(= i pos) (cons (car lst) lst)]
[else (cons (car lst) (dup (+ i 1) (cdr lst)))])))
(duplicate-shorthand 1 (list "apple" "cheese burger!" "banana"))
(define (duplicate-longhand pos lst)
(letrec ([dup (lambda (i lst)
(cond
[(= i pos) (cons (car lst) lst)]
[else (cons (car lst) (dup (+ i 1) (cdr lst)))]))])
(dup 0 lst)))
(duplicate-longhand 1 (list "apple" "cheese burger!" "banana"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment