Last active
August 29, 2015 14:27
-
-
Save umanomata/99f103ed686acd523d9e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The problem of time is because of the delete-duplicates procedure that runs in time O(n^2) for n-element lists.