PasteRack.org
Paste # 78658
2014-09-02 14:14:00

Forked from paste # 31618.

Fork as a new paste.

Paste viewed 97 times.


Embed:

Finding alphabetic words 3 (thanks vraid!)

  1. #lang racket
  2.  
  3. ; Given a File containing a newline-delineated list of English words, find the number of words
  4. ; whose individual letters are in alphabetical order.
  5.  
  6. ; Word -> List
  7. ; takes a Word and turns it into a list of that word's characters as 1-char strings.
  8. (define (listify w)
  9.   (map string (string->list w)))
  10.  
  11. ; Word -> Boolean
  12. ; Checks a Word and determines whether the letters are in alphabetical order
  13. (define (alphabetic? w)
  14.   (let ([word (string-downcase w)])
  15.     (equal? (listify word) (sort (listify word) string<?))))
  16.  
  17. ; Word -> Boolean
  18. ; Checks a Word to see if it is a valid word, ie. contains both consonants and vowels
  19. (define (real-word? w)
  20.   (memf (lambda (c) (member c '("a" "e" "i" "o" "u" "y"))) (listify w)))
  21.  
  22. ; String String -> String
  23. ; Takes two strings, returns the largest
  24. (define (string-max s1 s2)
  25.   (if (>= (string-length s1) (string-length s2))
  26.       s1
  27.       s2))
  28.  
  29. ; List -> Word
  30. ; Finds the longest word in a list of words
  31. (define (longest lst)
  32.   (foldl string-max "" lst))
  33.  
  34. ; Filename -> List
  35. ; Iterates over the given file, building a list of words which are alphabetic
  36. (define (alphabetic-in-file word-file)
  37.   (for/list ([w (in-lines (open-input-file word-file))]
  38.              #:when (and (alphabetic? w)
  39.                          (real-word? w)))
  40.     w))
  41.  
  42. ; List -> Lists
  43. ; Outputs a report containing useful information about the alphabetic list
  44. (define (alpha-report word-file)
  45.   (let ([lst (alphabetic-in-file word-file)])
  46.     (displayln `(Alphabetic words in file: ,(length lst)))
  47.     (displayln `(Longest alphabetic word in file: ,(longest lst)))
  48.     (displayln `(Number of alphabetic words with greater than 6 letters:
  49.                         ,(length (filter (lambda (s) (> (string-length s) 6)) lst))))
  50.     (displayln `(List of alphabetic words with greater than 6 letters:
  51.                         ,(filter (lambda (s) (> (string-length s) 6)) lst)))
  52.     (displayln `(Number of alphabetic words with greater than 3 letters:
  53.                         ,(length (filter (lambda (s) (> (string-length s) 3)) lst))))
  54.     (displayln `(List of alphabetic words: ,lst))))
  55.  
  56. ;; Sample Output Report
  57. ; Using the file "wordsEn.txt" gained from http://www-01.sil.org/linguistics/wordlists/english/
  58. ; (alpha-report "wordsEn.txt") produces the following report:
  59. ; (Alphabetic words in file: 544)
  60. ; (Longest alphabetic word in file: billowy)
  61. ; (Number of alphabetic words with greater than 6 letters: 2)
  62. ; (List of alphabetic words with greater than 6 letters: (beefily billowy))
  63. ; (Number of alphabetic words with greater than 3 letters: 351)
  64. ; (List of alphabetic words: (a aah aahs ab abbe abbes abbess abbey abbot abbott abbr abc abet abhor abhors ably abo abort abs abt abuzz ac accent accept access accost acct ace aces achoo achy aclu act ad add adder adders adds adept adios adit adj ado adopt ados ads adv adz aegis aery affix afflux afoot aft agin agist aglow ago ah ahoy ahs ai ail ails aim aims air airs airy al all allot allow alloy alls ally almost alms alp alps alt am ammo ammos amort amp amps amu an ann anno annoy ant any app apt ars art arty as ass asst at atty aux aw ax ay be bee beef beefily beefs beefy been beep beeps beer beers beery bees beet befit beg begin begins begirt begot begs bel bell bello bellow bells belly below belt ben benny bens bent berry bess best bet betty bevy bey bijou bijoux bill billow billowy bills billy bin bins bio biopsy bios bit bitty bloop bloops blot blotty blow blowy boo boor boors boos boost boot booty bop bops bort borty bortz boss bossy bot bow box boxy boy buy buzz by cees ceil ceils cell cello cellos cells celt cent cert cess chi chill chills chilly chimp chimps chin chino chinos chins chintz chip chippy chips chit chivvy chivy choosy chop choppy chops chou chow cit city civvy clop clops clot clotty cloy co coo coop coops coopt coos coot cop cops copy cost cosy cot cow coy cpu crux cry de deem deems deep deeps deer deers dees deft defy dei deist deity del dell dells delly demo demos den dens dent deny dept der des deux dew dewy dhow dill dills dilly dim dims din dins dint dip dippy dips dipt dirt dirty dis ditty divvy do door doors dopy dors dory dos doss dost dot dotty doty doxy doz dry eel eels eely eery efflux effort effs eft egg eggs egis ego egos eh el ell ells elm elms elmy emmy empty ems emu en enow ens envy err errs erst es ess et ex fill fills filly film films filmy fin finn finns finny fins fir firry firs first fist fit fix fizz floor floors flop floppy flops floss flossy flow flu flux fly foot footy fop fops for fort forty fox foxy fry fuzz ghost ghosty gill gills gilly gilt gimp gimps gimpy gin ginny gins gip gips gipsy girt gist git glop glops glory gloss glossy glow gnu go goo goop goops goos goosy gorsy gory got gov goy guy hi hill hills hilly hilt him hinny hint hip hippy hips his hiss hist hit ho hoop hoops hoot hop hops hor hors horst horsy host hot how hwy ii iii ill ills illy imp imps in inn inns ins inst int iou iqs irs is it iv ivy joss jot jotty joy knot knotty know knox lo loo loop loops loopy loos loot lop loppy lops lorry lory loss lossy lost lot low lox lux mo moo moor moors moory moos moot mop mops mopy mort moss mossy most mot mow mux my no nor nos nosy not now nu ny oops opp ops opt or ors ort os ow ox oxy oz pry qty sty tty tux))

=>