for loop: any way to access the whole list?

47 views
Skip to first unread message

Sanjeev Sharma

unread,
Jun 14, 2019, 10:45:10 AM6/14/19
to Racket Users
within this for loop is there any way to access different pieces of the description and amt?  car-ing and cdr-ing for example?

Or move the identifier definitions into the let*, and pass those to for in some way?

(let*((ratio 9/12))
  (for((description(list "this" "that"))
       (amt(list 4467.61 2428.37)))
    (printf"~a ~a: changed values\n"
           description
           (cat(* amt ratio) -2.))))

Sanjeev Sharma

unread,
Jun 14, 2019, 10:45:43 AM6/14/19
to Racket Users
cat is from srfi/54

Jens Axel Søgaard

unread,
Jun 14, 2019, 11:25:31 AM6/14/19
to Sanjeev Sharma, Racket Users
I am not entirely sure this answer is what you are looking for, but the following generator
will produce the pairs of the list instead of the elements. 

(define (in-pairs xs)
  (make-do-sequence
   (λ ()
     (define (pos->element p) p)
     (define (next-position p) (cdr p))
     (define initial-position xs)
     (define (continue-with-pos? p) (pair? p))
     (values pos->element
             next-position
             initial-position
             continue-with-pos?
             #f #f))))
Example:
> (for ([p (in-pairs '(1 2 3 4))])
    (displayln p))
(1 2 3 4)
(2 3 4)
(3 4)
(4)
So you can use 

(define descriptions (list "this" "that"))
(define amounts      (list 4467.61 2428.37))
(for ([d descriptions] [ds (in-pairs descriptions)])
      [a amounts]      [as (in-pairs amounts])
   ...)

And use ds and as to get to other descriptions/amounts than the current (which are in d and a).

/Jens Axel



--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/07ad5ce3-0ba8-48a5-a356-5cb1545de4cd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
--
Jens Axel Søgaard

David Storrs

unread,
Jun 14, 2019, 12:46:54 PM6/14/19
to Sanjeev Sharma, Racket Users
On Fri, Jun 14, 2019 at 10:45 AM Sanjeev Sharma <thro...@gmail.com> wrote:
within this for loop is there any way to access different pieces of the description and amt?  car-ing and cdr-ing for example?

I'm not entirely clear on what you're looking for, but maybe this helps?

(define lst '(a b c))
(for ([(val idx) (in-indexed lst)])
  (displayln (cons  val idx))
  (when (< idx (sub1 (length lst)))
    (displayln (format "\tnext val: ~a" (list-ref lst (add1 idx))))))

Output:
(a . 0)
        next val: b
(b . 1)
        next val: c
(c . 2)


Or move the identifier definitions into the let*, and pass those to for in some way?

(let*((ratio 9/12))
  (for((description(list "this" "that"))
       (amt(list 4467.61 2428.37)))
    (printf"~a ~a: changed values\n"
           description
           (cat(* amt ratio) -2.))))

--

Sanjeev Sharma

unread,
Jun 15, 2019, 9:00:15 AM6/15/19
to Racket Users
thanks, 

the whole idea is to localize the identifiers to the for (preferred) or the let*. 

Reaching back to get the whole list when the for has bound the same identifier is probably asking a bit much.

Within the given strictures I probably need to bind the list in the let* and use a different identifier in the for().


On Friday, June 14, 2019 at 12:46:54 PM UTC-4, David Storrs wrote:
On Fri, Jun 14, 2019 at 10:45 AM Sanjeev Sharma <thro...@gmail.com> wrote:
within this for loop is there any way to access different pieces of the description and amt?  car-ing and cdr-ing for example?

I'm not entirely clear on what you're looking for, but maybe this helps?

(define lst '(a b c))
(for ([(val idx) (in-indexed lst)])
  (displayln (cons  val idx))
  (when (< idx (sub1 (length lst)))
    (displayln (format "\tnext val: ~a" (list-ref lst (add1 idx))))))

Output:
(a . 0)
        next val: b
(b . 1)
        next val: c
(c . 2)


Or move the identifier definitions into the let*, and pass those to for in some way?

(let*((ratio 9/12))
  (for((description(list "this" "that"))
       (amt(list 4467.61 2428.37)))
    (printf"~a ~a: changed values\n"
           description
           (cat(* amt ratio) -2.))))

--
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...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages