Re: [racket-users] How to return summary from the inner loop?

2015-07-10 Thread Pierpaolo Bernardi
On Fri, Jul 10, 2015 at 11:11 PM, Stephen Chang  wrote:
> Perhaps I don't understand the problem, but why is the inner loop necessary?

He asked how to test each value against each of the limit pairs.  I
assume this is a simplified/demo problem and in the problem he really
cares about there's a reason for wanting this. Were it a real problem,
I'd suggest other solutions too :)

-- 
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.


Re: [racket-users] How to return summary from the inner loop?

2015-07-10 Thread Stephen Chang
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
 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.


Re: [racket-users] How to return summary from the inner loop?

2015-07-10 Thread Pierpaolo Bernardi
On Fri, Jul 10, 2015 at 10:45 PM, Pierpaolo Bernardi
 wrote:
> If I understand correctly the spec, this should do what you ask?

which, fixing the typo, becomes:

(define (validate-ranges value-list low-list high-list)
  (for/fold ((failures 0))
((low (in-list low-list))
 (high (in-list high-list)))
(for/fold ((failures 0))
  ((value (in-list value-list)))
  (if (<= low value high)
failures
(add1 failures)

-- 
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.


Re: [racket-users] How to return summary from the inner loop?

2015-07-10 Thread Pierpaolo Bernardi
If I understand correctly the spec, this should do what you ask?

(define (validate-ranges value-list low-list high-list)
  (for/fold ((failures 0))
((low (in-list value-list))
 (high (in-list high-list)))
(for/fold ((failures 0))
  ((value (in-list value-list)))
  (if (<= low value high)
failures
(add1 failures)



On Fri, Jul 10, 2015 at 10:01 PM, Pekka Niiranen
 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.


[racket-users] How to return summary from the inner loop?

2015-07-10 Thread Pekka Niiranen

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.