I'd write an implementation based on filter
from SRFI-1 (as suggested by @Sylwester) and SRFI-26, because I happen to like the cut
macro for currying functions. It'll yield a shorter and IMHO clearer answer, just make sure that all the words are in lower (or upper) case:
(use-modules (srfi srfi-1) (srfi srfi-26))
(define (arrange-by-occurrence sentence random-words)
(filter (cut memq <> random-words) sentence))
For example:
(arrange-by-occurrence '(the game is played on a level playing field)
'(played is the))
=> (the is played)
How does it work? Simple, filter
will traverse the original sentence in order and for each word it tests whether it's present in the random list of words - using memq
for that. Only those words present in the random list will be selected, and they'll be returned in the output list in the same order that they were found in the original sentence.