0

enter image description here

Why last expression stuck forever(i guess it will never stop) in nrepl. I have to Ctrl+c Ctrl+c to stop it.

yeh
  • 1,496
  • 14
  • 35

3 Answers3

3

Because you are trying to evaluate an infinite sequence (by printing it to the REPL).

The result of e.g. (filter #(> % 100) (iterate #(+ % 17) 0)) can be printed to the REPL, because the REPL will print the first x elements of the resulting sequence, followed by ..., where x is a value you can set with (set! *print-length* x).

But trying to evaluate(filter #(< % 100) (iterate #(+ % 17) 0)) will run forever, since there are only 6 possible elements.


Have a look at the *print-length* documentation:

;; Oops! Don't this!!!
user=> (iterate inc 0)
;; Frantically doing C-c C-c :-P
; Evaluation aborted.

user=> (set! *print-length* 10)
10

;; Now it's perfectly fine. Yay!
user=> (iterate inc 0)
(0 1 2 3 4 5 6 7 8 9 ...)

You may want to use take-while instead of filter, since the sequence you create with iterate is already ordered.

user=> (take-while #(< % 100) (iterate #(+ % 17) 0))
(0 17 34 51 68 85)
sloth
  • 99,095
  • 21
  • 171
  • 219
  • I was asking why finite results won't be printed out. Just because its length is less than *print-length*? Is it weird? – yeh Apr 10 '13 at 07:14
  • 1
    There is no finite sequence here, iterate is infinite, running filter on infinite sequence will result in infinite sequence. – Ankur Apr 10 '13 at 07:16
  • 1
    *Just because its length is less than print-length?* The REPL just tries to get the first `x` elements. There's no way to know for the REPL how much time it takes to get those elements, or if it is possible at all. Also, your sequence is not finite. – sloth Apr 10 '13 at 07:17
  • So, what if i want to generate numbers that are less than for example 100 but are multiples of 5? That is my initial goal. – yeh Apr 10 '13 at 07:21
  • @easterbunny You can use `take-while` instead of filter. See my update. – sloth Apr 10 '13 at 07:42
0

As shown in the above expressions response the printed sequence is not complete result (check the ... in the end of the sequence), this indicates that nrepl when returning the response of a sequence take specific number of items from the sequence and print them with a ... to indicate that there are many more in the sequence. In your last case the "less then 100 numbers" won't produce the minimum number of items required by nrepl to print and hence nrepl keeps on waiting for more items from the sequence (which is an infinite sequence becoz of iterate)

Ankur
  • 33,367
  • 2
  • 46
  • 72
0

There are only 3 numbers in your sequence that are less than 100. However, since it is "infinite", filter must look at an "infinite" number of elements to determine that the fourth element does not exist.

Since your example iterate construct generates an increasing sequence, you could use take-while if you want to limit the results to elements less than 100. For example:

(take-while #(< % 100) (iterate #(+ % 17) 0))
Nathan Davis
  • 5,636
  • 27
  • 39