4 ms·
Racket is quite a lot more than call/cc and macros. By the way, FOR loops invite off-by-one errors and worse. Use a combinator like map, or filter or foldr et
by eru 12y ago
Racket is quite a lot more than call/cc and macros.
By the way, FOR loops invite off-by-one errors and worse. Use a combinator like map, or filter or foldr etc, to keep things boring.
- Sanddancer 12y agoFor loops in perl have two styles. The first is the c-style loop, the other is a map. One often uses the latter style a lot more often.
- pyre 12y agoThat, and Perl has an actual `map` function too: map { $_ + 1 } (@list);
- huehue 12y agoSometimes Perl can be a beauty: sub sum_of_squared_pairs { reduce { $a + $b } map { $_ * $_ } grep { $_ % 2 == 0 } @_ } sub schwartzian_transform { map { $_->[0] } sort { $a->[1] <=> $b->[1] } # use numeric comparison map { [$_, length $_] } # calculate the length of the string @_ }
- eru 12y agoYes, but _ has dynamic scope (I believe). That's very dangerous in general.
- minikomi 12y agoThere are also the extremely "boring" & practical list comprehensions / iterators within racket itself: http://docs.racket-lang.org/reference/for.html http://docs.racket-lang.org/reference/for.html eg. fizzbuzz using for, match: -> (for ([i (range 1 16)]) (match (list (modulo i 3) (modulo i 5)) [(list 0 0) (displayln "fizzbuzz")] [(list 0 _) (displayln "fizz")] [(list _ 0) (displayln "buzz")] [_ (displayln i)])) 1 2 fizz 4 buzz fizz 7 8 fizz buzz 11 fizz 13 14 fizzbuzz
- ktg 12y agoIn for, in-range is faster. http://docs.racket-lang.org/reference/sequences.html?q=in-range#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._in-range%29%29 http://docs.racket-lang.org/reference/sequences.html?q=in-ra... An in-range application can provide better performance for number iteration when it appears directly in a for clause. -> (for ([i (in-range 1 16)]) (match (list (modulo i 3) (modulo i 5)) [(list 0 0) (displayln "fizzbuzz")] [(list 0 _) (displayln "fizz")] [(list _ 0) (displayln "buzz")] [_ (displayln i)]))
- minikomi 12y agoOh, thanks for that :)