📜 ⬆️ ⬇️

10 Racquet line-ups that will impress your friends

I want to return to the forgotten topic about the “10 one-liners” in my favorite language. Vigorous discussion on Habré was a few months ago . Unfortunately, not many of them were really single line readers and not many of them were readable. I want to offer a version on Racket, which in some ways even surpasses the original Scala example in these parameters.


1. We double each element of the list

You can do this:
(for/list ((i (in-range 10))) (* 2 i)) 

Either way:
 (map (curry * 2) (build-list 10 (λ (x) x))) 


2. Sum all the numbers in the list

 (apply + (build-list 10000 (λ (x) x))) 

')
3. Check the occurrence of a substring

 (define wordlist '("Racket" "akka" "play framework" "sbt" "curring")) ;   (define tweet "This is an example tweet talking about Racket and curring") ;   

Finds the first occurrence (if not found, returns #f):
 (findf (curryr regexp-match? tweet) wordlist) 

Selects from wordlist substrings present in the tweet:
 (filter (curryr regexp-match? tweet) wordlist) 


4. Reading from file

 (file->string "file.txt") ;    (file->lines "file.txt") ;    


5. Print the song "Happy Birthday"


Once option
 (for-each (λ (x) (display (string-append "Happy Birthday" (if x ", dear John" " to You") "\n"))) '(#f #f #t #f)) 

Two options
 (for ((x (in-range 4))) (display (string-append "Happy Birthday" (if (= x 2) ", dear John" " to You") "\n"))) 

Three options
 (display-lines (build-list 4 (λ (x) (string-append "Happy Birthday" (if (= x 2) ", dear John" " to You"))))) 


6. Filtering the list of numbers

 (partition (curry > 60) '(49 58 76 82 88 90)) 

Returns two lists - matching the condition and not responding.

7. Receiving and parsing XML from a web service

Load the standard libraries:
 (require net/url xml) 

The following action places the resulting xml into a document type structure
 (define doc (read-xml (get-pure-port (string->url "http://search.twitter.com/search.atom?&q=racket")))) 

If you wish, you can parse the resulting document into a tree — the native format of the Lisp, which is Racket:
 (xml->xexpr (document-element doc)) 


8. Search for a minimum (or maximum) in the list

No more difficult than adding numbers:
 (apply max '(14 35 -7 46 98)) (apply min '(14 35 -7 46 98)) 


9. Parallel processing

Let there be some data. As a test, we give the usual list:
 (define data-list '(ABCDEFGH)) 

And there is some kind of procedure. For clarity, choose the multiple print symbol:
 (define (process sym) (for ((n (in-range 10))) (display sym))) 

Parallel processing starts like this:
 (for ((x data-list)) (thread (λ () (process x)))) 


10. Sieve of Eratosthenes

The task is rather algorithmic. There are many ways to solve it.
Here is the generation of a list of numbers from 2 to max
 (define (eratosphen max) (let er ([lst null] [cur 2]) (cond [(> cur max) (reverse lst)] [(ormap (λ (x) (= (remainder cur x) 0)) lst) (er lst (add1 cur))] [else (er (cons cur lst) (add1 cur))]))) 

This is not worth recording in one line.
And if you only need to check the number for simplicity using the sieve of Eratosthenes, then the code can really fit into the string (the checked number should be in the place of 113).
 (let check ([cur 2] [n 113]) (cond [(= (remainder n cur) 0) #f] [(> (sqr cur) n) #t] [else (check (add1 cur) n)])) 

But I would still break it “on the ladder”, like all other solutions to this problem.

Source: https://habr.com/ru/post/126606/


All Articles