Skip to content

Instantly share code, notes, and snippets.

@umanomata
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save umanomata/99f103ed686acd523d9e to your computer and use it in GitHub Desktop.
Save umanomata/99f103ed686acd523d9e to your computer and use it in GitHub Desktop.
;;; Program based on example code from composingprograms.com
;;;
;;; - Get a text document from the Web containing works of Shakespeare.
;;; - Count the words in the document and display the result.
;;; - Display the list of words that are six-letters long and that written
;;; backwards form a word that is also included in the document.
;;;
;;; NOTE: This code takes about 9 minutes to run in my machine. This is
;;; because of the delete-duplicates procedure whose execution time
;;; is O(n^2).
;;;
;;; Also note that the list of words obtained with string-split in
;;; this case does not include words only but also punctuation marks
;;; (the original code from the book does the same).
(use-modules (ice-9 receive)
(srfi srfi-1)
(web client)
(web uri))
(define text-url "http://composingprograms.com/shakespeare.txt")
(define words (receive (r-header r-body) (http-get (string->uri text-url))
(delete-duplicates (string-split r-body #\space))))
(begin (display (length words)) (newline))
(for-each
(lambda (word)
(if (and (= (string-length word) 6) (member (string-reverse word) words))
(begin (display word) (newline))))
words)
@umanomata
Copy link
Author

The problem of time is because of the delete-duplicates procedure that runs in time O(n^2) for n-element lists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment