gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
Hello! Please create a URL shortener in Racket please!
- User
Hello! Please create a URL shortener in Racket please!
- Assistant
Sure! Here's an example implementation of a URL shortener in Racket:
#lang racket ;; Define a dictionary to store the mappings between short and original URLs (define url-dictionary (hash)) ;; Function to generate a unique short URL (define (generate-short-url) (let loop ([short-url (random 100000)]) ; Generate a random number between 0 and 100000 (if (hash-has-key? url-dictionary short-url) (loop (random 100000)) ; If generated number already exists, generate a new one short-url))) ;; Function to shorten a given URL (define (shorten-url original-url) (define short-url (generate-short-url)) (hash-set! url-dictionary short-url original-url) short-url) ;; Function to retrieve the original URL from a shortened URL (define (get-original-url short-url) (hash-ref url-dictionary short-url #f)) ;; Generate a shortened URL for a given original URL (define shortened-url (shorten-url "https://www.example.com")) ;; Retrieve the original URL from the shortened URL (define original-url (get-original-url shortened-url)) (println "Shortened URL: " shortened-url) (println "Original URL: " original-url)