99 ways to say '(I love you)

[article index] [] [@mattmight] [rss]

In spite of their simplicity, lists often confound new Racket programmers.

After lists, the many forms for expressing a computation in Racket take time to discover and then master.

To address both of these points, I've created 99 roughly tweet-sized expressions in Racket that all evaluate to the list '(I love you).

About half of these demonstrate ways to construct or operate on lists, while the remainder showcase the many ways of expressing computation in Racket.

Key features touched include:

  • cons cells;
  • literal list notation;
  • dotted list notation;
  • quasiquoted lists;
  • let-binding forms;
  • vectors and streams;
  • anonymous functions;
  • higher-order list operations;
  • conditionals;
  • structural pattern-matching;
  • hash maps;
  • continuations;
  • exceptions;
  • promises;
  • mutation;
  • macros;
  • ports;
  • futures; and
  • parameters.

Read on for the 99.

When you encounter one that don't understand, play with it in the DrRacket REPL until you figure it out.

99 ways

Each of the following Racket expressions evaluates to the list '(I love you):

;;;
'(I love you)

;;;
(quote (I love you))

;;;
(list 'I 'love 'you)

;;;
(list (quote I) (quote love) (quote you))

;;;
(cons 'I (cons 'love (cons 'you '())))

;;;
(cons 'I (cons 'love (list 'you)))

;;;
(cons 'I (list 'love 'you))

;;;
(cons 'I '(love you))

;;;
'(I love . (you))

;;;
'(I . (love . (you)))

;;;
'(I . (love . (you . ())))

;;;
'(I . (love you))


;;;
`(I love you)

;;;
`(I ,'love you)

;;
(quasiquote (I ,'love  you))

;;
(quasiquote (I (unquote 'love) you))

;;;
`(I ,`love you)

;;;
(let ([verb 'love])
  `(I ,verb you))

;;;
`(I ,(string->symbol "love") you)

;;;
`(I ,(string->symbol (list->string '(#\l #\o #\v #\e))) you)

;;;
`(I love . (you))

;;;
`(I love . ,(list 'you))

;;;
`(I love ,@'(you))

;;;
`(I love (unquote-splicing '(you)))

;;;
`(I ,@(list 'love 'you))

;;;
`(,@(list 'I 'love) you)

;;;
`(,@'(I love you))

;;;
`,'(I love you)

;;;
`(I love you . ,'())

;;;
(car (list '(I love you)))

;;;
(cdr '(Hark! I love you))

;;;
(let ([words '(love you I)])
  (list (car (cdr (cdr words)))
        (car words)
        (car (cdr words))))

;;;
(let ([words '(love you I)])
  (list (caddr words)
        (car words)
        (cadr words)))

;;;
(let* ([c '(you)]
       [b (cons 'love c)]
       [a (cons 'I b)])
  a)

;;;
(let ()
  '(I love you not)
  '(I love you))


;;;
(vector->list (vector 'I 'love 'you))

;;;
(vector->list #(I love you))

;;;
(stream->list (stream 'I 'love 'you))


;;;
((lambda args args) 'I 'love 'you)

;;;
((lambda (one two . rest) rest) 'You 'believe 'I 'love 'you)

;;;
((lambda (a c b) (list a b c)) 'I 'you 'love)

;;;
(apply (lambda (a c b) (list a b c)) 
       (list 'I 'you 'love))

;;;
((lambda (a b [c 'you]) (list a b c)) 'I 'love)

;;;
((lambda (#:foo b #:bar c #:baz a)
   (list a b c))
 #:baz 'I #:bar 'you #:foo 'love)

;;;
((lambda (a b #:key [c 'me]) (list a b c)) #:key 'you 'I 'love)

;;;
(let ([f (λ (x)
           (λ (y)
             (λ (z)
               (list x y z))))])
  (((f 'I) 'love) 'you))


;;;
(let ([f (case-lambda 
           [() 'I]
           [(x) 'love]
           [(x y) 'you])])
  (list (f) (f 1) (f 1 2)))

    

;;;
(append '(I love) '(you))

;;;
(append '(I) '(love) '(you))

;;;
(flatten '((I) (love you)))

;;;
(flatten '((I) (love) (you) ()))

;;;
(reverse '(you love I))

;;;
(remove 'cannot '(I cannot love you))

;;;
(remove-duplicates '(I love love love you))

;;;
(take '(I love you not) 3)

;;
(take-right '(I think I love you) 3)
      
;;;
(drop '(She knows I love you) 2)

;;;
(drop-right '(I love you no more) 2)



;;;
(map (lambda (x) (if (eq? x 'hate) 'love x))
     '(I hate you))

;;;
(map (λ (i) (vector-ref #(love you I) i))
     '(2 0 1))

;;;
(map (λ (k) (hash-ref #hash(("foo" . I) 
                            ("baz" . you)
                            ("bar" . love)) k))
     '("foo" "bar" "baz"))
     
;;;
(map string->symbol (sort (list "love" "you" "I") string<?))

;;;
(map string->symbol (string-split "I-love-you" "-"))

;;;
(flatten (map (λ (a b) (cons a b))
              '(I love you)
              '(() () ())))


;;;
(filter (lambda (x) (not (eq? x 'cannot))) 
        '(I cannot love you))

;;;
(foldr cons '() '(I love you))

;;;
(foldl cons '() '(you love I))

;;;
(for/list ([word #(I love you)])
  word)


;;;
(cond
  [(even? 3) '(Not me)]
  [(odd?  3) '(I love you)])
  
;;;
(cond
  [(even? 3) '(Not me)]
  [(odd?  2) '(Nor I)]
  [else      '(I love you)])
  
;;;
(case 1
  [(a b c) '(Not me)]
  [(3 2 1) '(I love you)])




;;;
(match #t
  [#f '(Not me)]
  [#t '(I love you)])

;;;
(match #t
  [#f '(Not me)]
  [_  '(I love you)])

;;;
(match 'you
  ['me '(Not me)]
  [x   `(I love ,x)])

;;;
(match '(foo bar)
  ['(foo bar) '(I love you)])

;;;
(match '(I cannot lift you)
  [(list 'I 'cannot _ c) `(I love ,c)])

;;;
(match '(2 3 1)
  [(list-no-order 3 1 2)
   '(I love you)])

;;;
(match '(love you I)
  [(list-no-order 'I 'love foo)
   `(I love ,foo)])

;;;
(match '(3 . 4)
  [(cons 3 4)
   '(I love you)])

;;;
(match '(3 love 1)
  [(cons 3 (cons x (cons 1 '())))
   `(I ,x you)])

;;;
(match '(3 love 1)
  [(cons 3 (cons x (cons 1 '())))
   `(I (unquote x) you)])

;;;
(match 3
  [(? symbol?) '(Not me)]
  [(? string?) '(Me neither)]
  [(? number?) '(I love you)])

;;;
(match 3
  [(not 4)   '(I love you)]
  [3         'unreachable])

;;;
(match '(you love I)
  [`(,c love ,a)
   `(,a love ,c)])

;;;
(match '(We love you)
  [`(,_ . ,rest)
   `(I . ,rest)])

;;;
(match '(We love you)
  [`(,_ ,rest ...)
   `(I ,@rest)])

;;;
(match '(We love you)
  [(list _ rest ...)
   `(I ,@rest)])

;;;
(match #(1 love 3)
  [(vector (? number?) b (? number?))
   `(I ,b you)])

;;;
(match #hash((1 . I) (3 . you) (5 . love))
  [(hash-table (1 a) (5 b) (3 c))
   (list a b c)])

;;;
(match 'you
  [(and x (? symbol?)) `(I love ,x)])

;;;
(match '100
  [(app (λ (n) (- n 1)) 99)
   '(I love you)])


;;;
(list 'I 
      (call/cc (λ (cc)
                 (error (cc 'love))))
      'you)

;;;
(with-handlers ([symbol? (lambda (p)
                             `(I ,p you))])
    (raise 'love))


;;;
(let ([problem (delay (car '()))])
  '(I love you))

;;;
`(I ,(force (delay 'love)) you)
 
;;;
(letrec ([x (delay (list a b c))]
         [a 'I]
         [c 'you]
         [b 'love])
  (force x))
       


;;;
(let ([word 'know])
  (set! word 'love)
  `(I ,word you))

;;;
(let ([word-box (box 'know)])
  (set-box! word-box 'love)
  `(I ,(unbox word-box) you))


;;;
(let-syntax ([un-yoda-list
              (syntax-rules ()
                [(_ c a b) (list 'a 'b 'c)])])
  (un-yoda-list you I love))

;;;
(let ((in (open-input-string "I love you")))
  (cons (read in)
        (cons (read in)
              (cons (read in) '()))))

;;;
(list (touch (future (λ () 'I))) 'love 'you)


;;;
(let ([a (make-parameter "a")] 
      [b (make-parameter "b")]
      [c (make-parameter "c")])
  (parameterize ([a 'i] [b 'love] [c 'you])
    (list (a) 
          ((parameterize ([b 'dislike])
             (λ () (b))))
          (c))))

Contributions

From David Van Horn:

(define `λ (λ 'love 'I 'you)) 
((λ λ λ) 
  `(λ (⊙ λ ∪) λ) 
   `(λ (λ ⊙ ∪) λ)
   `(λ (∪ ⊙ λ) λ))

From Bruce Bolick:

(define (stream-take s n)
  (if (= n 0)
      '()
      (cons (stream-first s)
            (stream-take (stream-rest s) (- n 1)))))
(define U 3)
(define I stream-take)
(define ♥ (stream  'I 'love 'you))
(I ♥ U)

More resources

The Racket docs are a good place to start.

My research colleague David Van Horn has co-authored Realm of Racket, which teaches both programming and Racket via game programming:

Co-authored by Matthias Felleisen and Conrad Barski (of Land of Lisp fame), this is an excellent resource for learning.

Related pages