#lang racket ;; --------------------------------------------------------------------- ;; list ::= () ;; | (item . list) (define cons-at-end (lambda (v lst) (if (null? lst) (list v) (cons (first lst) (cons-at-end v (rest lst)))))) ;; --------------------------------------------------------------------- ;; Here are a couple of ways to do this without recursion: ;; ;; (reverse (cons v (reverse lst))) ;; (append lst (list v)) ;; --------------------------------------------------------------------- ;; Someone asked about the corresponding accessor, which has to return ;; everything in a list *except* its last value. ;; I sometimes call that all-but-last: (define all-but-last (lambda (lst) (if (null? (rest lst)) '() (cons (first lst) (all-but-last (rest lst)))))) ;; --------------------------------------------------------------------- ;; We can also do this without recursion: ;; ;; (reverse (rest (reverse lst)))