Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. (defun load-words (pathname)
  2. "loads words from pathname and sorts them into an array of vectors by length then
  3. returns that array. A word of length 5 will be found in position (aref array (1- (length word)))"
  4. (labels ((get-words (pathname);;gonna just pass over the words twice, design decision
  5. (let ((words (make-array 0 :element-type 'string :adjustable t :fill-pointer 0)))
  6. (with-open-file (file pathname)
  7. (loop
  8. :for line := (read-line file nil)
  9. :for len := (length line)
  10. :with max := 0
  11. :while line
  12. :do (vector-push-extend line words)
  13. :when (> len max)
  14. :do (setf max len)
  15. :finally (return (values words max))))))
  16. (sort-words (words max)
  17. (let ((sorted (map-into (make-array max :element-type 'vector
  18. :initial-element #()) (lambda ()
  19. (make-array 0 :
  20. element-type 'string
  21. :adjustable t
  22. :fill-pointer t)))))
  23. (loop
  24. :for word :across words
  25. :for len := (length word)
  26. :do (vector-push-extend word (aref sorted (1- len)))
  27. :finally (return sorted)))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement