Skip to content

Instantly share code, notes, and snippets.

@wilbowma
Created January 26, 2022 05:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wilbowma/b43859181851b6a1eae4590d9ed9316f to your computer and use it in GitHub Desktop.
Save wilbowma/b43859181851b6a1eae4590d9ed9316f to your computer and use it in GitHub Desktop.
#lang racket
(require (rename-in racket/base [+ r:+]))
(provide +)
(define (join e1 e2)
(cond
[(and (number? e1) (number? e2))
number?]
[(and (number? e1) (symbol? e2))
symbol?]
[(and (number? e1) (string? e2))
string?]
[(and (number? e1) (void? e2))
number?]
[(and (symbol? e1) (number? e2))
symbol?]
[(and (symbol? e1) (symbol? e2))
symbol?]
[(and (symbol? e1) (string? e2))
string?]
[(and (symbol? e1) (void? e2))
string?]
[(string? e1)
string?]
[(string? e2)
string?]))
(define (to f e)
(match f
[(? (curry eq? number?))
(if (void? e) 0 e)]
[(? (curry eq? symbol?))
(string->symbol (format "~a" e))]
[(? (curry eq? string?))
(format "~a" e)]))
(define (add e1 e2)
(match (join e1 e2)
[(? (curry eq? number?))
(r:+ (to number? e1) (to number? e2))]
[(? (curry eq? symbol?))
(lambda (e1 e2) (string-append (symbol->string (to symbol? e1))
(symbol->string (to symbol? e2))))]
[(? (curry eq? string?))
(string-append (to string? e1) (to string? e2))]
[else (error "meow" (join e1 e2))]))
(define +
(lambda rest
(cond
[(null? rest)
(void)]
[(null? (cdr rest))
(car rest)]
[else
(add (add (car rest) (cadr rest)) (apply + (cddr rest)))])))
(module+ test
(require rackunit)
(check-equal? (+ 2 2) 4)
(check-equal? (+ 2 "") "2")
(check-equal? (+ "hello" " " " world") "hello world")
(check-equal? (+ "hello" " " 'world) "hello world")
(check-equal? (+ "hello" 4 'world) "hello4world")
(check-equal? (+ 4 'world) '4world))
#lang sweet-exp racket
require "meow.rkt"
{"Hello, " + "world." + " What have you done." + " What did you do to my parens?!"}
{"Hello, " + 2 + 'lol}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment