PasteRack.org
Paste # 27343
2019-12-17 09:08:15

Fork as a new paste.

Paste viewed 706 times.


Embed:

  1. #lang racket
  2.  
  3. (define (gypsy keyword sentence)
  4.   (let ([result
  5.          (foldl (lambda (str1 str2)
  6.                   (string-append str1 " " str2))
  7.                 ""
  8.                 (map glue-phrase
  9.                      (let ([words (filter pair? (sentence->syllables sentence))]
  10.                            [keyword (word->syllables keyword)])
  11.                        (let loop ([words words]
  12.                                   [processed null])
  13.                          (match words
  14.                                 [`(,x ,y . ,z)
  15.                                  (loop (cdr words)
  16.                                        (cons (encode-word y x) processed))]
  17.                                 [`(,x . ,z) #:when (null? z)
  18.                                  (cons (encode-word keyword x) processed)]
  19.                                 [_
  20.                                  processed])))))])
  21.     (substring result 0 (- (string-length result) 1))))
  22.  
  23. (define (encode-word keyword word)
  24.       (cons (cons (car keyword) (cdr word))
  25.             (list (cons (car word) (cdr keyword)))))
  26.  
  27. (define (glue-phrase phrase)
  28.   (string-append
  29.    (apply string-append
  30.           (car phrase))
  31.    " "
  32.    (apply string-append
  33.           (cadr phrase))))
  34.  
  35. (gypsy "груша" "Католическая философия со времен Августина до эпохи возрождения")

=>

sentence->syllables: undefined;

 cannot reference an identifier before its definition

  in module: 'm