Perhaps I don't understand the problem, but why is the inner loop necessary?

By the way, you can use (printf ...) instead of (fprintf
(current-output-port) ...)

Would this work?

(define (validate-ranges2 value-list low-list high-list)
  (for/sum ([v value-list]
            [lo low-list]
            [hi high-list]
            #:when (< lo v hi))
    (printf "\n*** Faulty!\n")
    1))

Or, without using for:

(define (validate-ranges3 vals los his)
  (count
   (λ (lo v hi)
     (and (< lo v hi)
          (printf "\n*** Faulty!\n")))
   los vals his))

Without the printing:

(define (validate-ranges4 vals los his)
  (count < los vals his))


On Fri, Jul 10, 2015 at 4:01 PM, Pekka Niiranen
<pekka.niira...@pp5.inet.fi> wrote:
> Hello users,
>
> I want to loop thru values and return the total number of failures
> instead of breaking out of loop as soon as the first failure is encountered.
>
> The code below works as intended:
>
> define (validate-order value-list low-list)
>   ;; Checks whether all the values are all above list of low limits
>   (define result
>     (for/fold ([failures 0])
>               ([e low-list]
>                [v value-list])
>       (if (< v e)
>           (begin
>             (fprintf (current-output-port) "BS~n")
>             (add1 failures))
>           failures)))
>   result)
>
>
> However, if I need to use nested loop I have to build
> intermediate list "ranges" before looping:
>
> (define (validate-ranges value-list low-list high-list)
>   ;; Checks whether all the values are all between list
>   ;; of low/high limits
>
>   ;; I do not like this intermediate definition:
>   (define ranges (for/list ([s low-list]
>                             [e high-list])
>                    (cons s e)))
>
>   (define result
>     (for/fold ([failures 0])
>               ([v value-list]
>                [r ranges])
>
>       ;; I do not like the car/cdr -function calls either:
>
>       (if (< (car r) v (cdr r))
>           (begin
>             (fprintf (current-output-port) "~n")
>             (fprintf (current-output-port) "*** Faulty!~n")
>             (add1 failures))
>           failures)))
>   result)
>
> Is is somehow possible to return the result from the inner
> loop to the scope of the outer loop? Somehow like this:
>
> (define (validate-ranges value-list low-list high-list)
>   (define result (for/??? ([s low-list]
>                            [e high-list])
>                           (for/fold ([failures 0])
>                                     ([v value-list])
>                             (if (< s v e)
>                                 (begin
>                                   (fprintf (current-output-port) "BS~n")
>                                   (add1 failures))
>                                 failures))))
>     result)
>
> -pekka-
>
> --
> You received this message because you are subscribed to the Google Groups
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to