Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (defun load-words (pathname)
- "loads words from pathname and sorts them into an array of vectors by length then
- returns that array. A word of length 5 will be found in position (aref array (1- (length word)))"
- (labels ((get-words (pathname);;gonna just pass over the words twice, design decision
- (let ((words (make-array 0 :element-type 'string :adjustable t :fill-pointer 0)))
- (with-open-file (file pathname)
- (loop
- :for line := (read-line file nil)
- :for len := (length line)
- :with max := 0
- :while line
- :do (vector-push-extend line words)
- :when (> len max)
- :do (setf max len)
- :finally (return (values words max))))))
- (sort-words (words max)
- (let ((sorted (map-into (make-array max :element-type 'vector
- :initial-element #()) (lambda ()
- (make-array 0 :
- element-type 'string
- :adjustable t
- :fill-pointer t)))))
- (loop
- :for word :across words
- :for len := (length word)
- :do (vector-push-extend word (aref sorted (1- len)))
- :finally (return sorted)))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement