How to apply a for loop to iterate through a list!

121 views
Skip to first unread message

Timothy Malan

unread,
Jun 24, 2023, 9:27:17 AM6/24/23
to Pyret Discuss
Hello everyone!

New coder here!  I'ld like to apply a for loop to iterate through a list but they are formatted in a way that I cannot understand.

For practice I'm looking at school student test score data and I'd like to iterate through a list of races and assign total students to each list in a new dictionary.  I then plan to iterate through the dictionary to make a new table.

My loops are:
for elem from list-of-ethnicities:
          ethnicity-sum = sum(schools-table,elem)
           ethnicity-sum-dict.set-now(elem,ethnicity-sum)
end

&

#adds row to table
for elem from list-of-ethnicities):
       row = total-ethnicities-table.row(elem, ethnicity-sum-dict.get-value-now(elem))
       total-ethnicities-table.add-row(row)
end

what I'm confused by it seems like pyret won't let me iterate through a list without applying a function at the beginning of the loop, like map or fold.

below is a link to the code.

Thank you!

Shriram Krishnamurthi

unread,
Jun 24, 2023, 9:37:00 AM6/24/23
to pyret-...@googlegroups.com
Hi Timothy,

That's correct. Pyret's `for` loop is really just a syntactically comfortable way of using higher-order functions.

You could write

for each(…)

and get most of what you want. `each` is itself a function, like `map` or `fold`, but it is specifically designed to let you do this sort of thing and "look familiar".

However, around here we do annoying things like asking, "What exactly are you trying to accomplish?" Why are you copying things from one directory to another? That's not usually a useful thing to do; it may seem necessary for accomplishing some goal, but often there are different/better ways of accomplishing that end goal. So we like to focus on the goal and then figure out a computational structure that achieves it well (and idiomatically).

Shriram

Timothy Malan

unread,
Jun 25, 2023, 4:57:01 PM6/25/23
to Pyret Discuss
Hi Shriram,

That's not an annoying question that is a great question, because if forces critical thinking and learning on my part.

I have a table with a bunch of rows that consist of schools and student numbers for different ethnicities.  I was trying to write a program that would iterate through each ethnicity and calculate the sum with the sum function.

I then put those results into a dictionary.  I was going to do a separate iteration of the dictionary items into new rows in a table with the .add-row method.

One reason I was doing this was to challenge myself.  I am a novice coder and wanted to practice my for loops.

The other reason I did this was because I don't know how to iterate in a table data type.

If you have any suggestions I am open ears!  Thanks for your help!

Shriram Krishnamurthi

unread,
Jun 25, 2023, 5:22:27 PM6/25/23
to pyret-...@googlegroups.com
Great. Let's work through this.

The Design Recipe would tell you to first write some concrete examples of your data. Can you write two small but useful tables that illustrate the kind of data you're trying to process?

Timothy Malan

unread,
Jul 8, 2023, 1:52:39 PM7/8/23
to Pyret Discuss
Hi Shriram,

Below is my attempt to simply it and put it in the design recipe.

Screen Shot 2023-07-08 at 10.10.50 AM.png
I removed the dictionary and am trying to create a table now using a single for loop.  I did not find a way to remove the list at the beginning because the second parameter of sum needs a string and I'm not sure how I can call the desired column headers in the table with a function.  Putting them in a list just seemed easier.
Here is my most recent attempt:
Screen Shot 2023-07-08 at 10.49.13 AM.png


Joe Gibbs Politz

unread,
Jul 19, 2023, 1:45:43 AM7/19/23
to pyret-...@googlegroups.com
Hi Timothy,

A lot of good ideas here. In the version you shared I also see a pretty good use of `.row` (the screenshot uses raw-row, which could work, but with incorrect syntax. Using .row is a good idea).

I have two kinds of answers for you: (a) the next pedagogic steps based on what Shriram was building up, and (b) some direct remarks to get you unstuck.

I'll focus a little more on (b) with a dash of (a). From looking at your code and the output, I think the dictionary likely has correct results in it, but I can't actually tell. I'd want a shorter example to figure it out and gain confidence that those are the right sums before e.g. publishing it to a class or sharing it with a policy-maker.

One way to approach that is to write a function that takes not just a table, but also a list of the columns to sum, and produces such a dictionary (this is what you've done in top-level code). Then, you could check a few small examples to make sure the sums are producing the results you expect. I think this would be productive and likely make someone *reading* the code understand it better. You could also take another state's data and apply the same function to it, etc.


In terms of the table-based approach you have at the end, there's a difference between how mutable-string-dict and tables work. When Pyret evaluates `set-now` on a MSD, it changes the dictionary “in-place”. When Pyret evaluates `.add-row` on a table, it creates a *whole new table* with the row added.

Without telling you what to write, I think there are a few things I'd suggest:

- Try framing this as a function from a mutable string dict to a table, and think about some examples
- If you get to the point of writing it, there are a few approaches to try:
  - One involves trying out for with fold (https://pyret.org/docs/latest/lists.html#%28part._lists_fold%29)

Overall, great approaches you've arrived at already. I think organizing code into functions so you can write and try small examples, and then trying out some of these other approaches, will give you a productive way forward.

Thanks for the great questions – please follow up with more as they come up!








--
You received this message because you are subscribed to the Google Groups "Pyret Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyret-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyret-discuss/5c255fff-0571-4d76-95f6-d38a40406c44n%40googlegroups.com.

Timothy Malan

unread,
Sep 8, 2023, 12:42:12 AM9/8/23
to Pyret Discuss
Hey everyone,

Sorry I've been for a while.  So I'm trying to apply Pyret in a practical way into the classroom and I'm coming up with some of the same issues and frustrations I did previously.

I feel like if Pyret is supposed to be an education language, then it's functions should be practical.  For example, in data analysis my students always find the average of a column in a data table.  Why can Pyret not do this?  I find it incredibly frustrating.

The mean function applies to a list.  So every time I want them to find the mean we have to convert our table to a list.  It doesn't make sense to me.

So for my project I need help calculating a column of successful times dropped by a parachute in a table into a mean so that mean can be compared to an experimental parachute.  I would love some help on this.


I'm again trying a for loop to iterate over each number and convert it to a list.  I'm storing the time as "value" and then the only way I see to build a list is the append function but it needs to be two lists.  So I'm trying to put "value" in [list:value] but I don't think Pyret understands that inside that list is a variable.  I'm guessing it thinks I'm entering the string "value".

Anyways thanks for listening to my rant.  Sorry to vent and I really do appreciate all your help.

Shriram Krishnamurthi

unread,
Sep 11, 2023, 11:43:20 AM9/11/23
to pyret-...@googlegroups.com
Hi Timothy,

Thanks for sharing your code — and your frustration.

I'm happy to show you how to do what you want. Can you tell me which column you want to average out of all these tables?
Shriram

Timothy Malan

unread,
Sep 11, 2023, 2:47:31 PM9/11/23
to Pyret Discuss
Hi Shiram,

Thank you so much for the support.  I'm excited to learn from you.  I am trying to find the mean of the time column.  While you are giving me some feedback can I also have support with rounding the average to the nearest hundredth if that's possible?

Also, could you give me some feedback on my attempts to apply Pyret to the data analysis process?  As you know I've been struggling a little bit.  Do you think I'm missing the main purpose of Pyret?  I guess I'm trying to duplicate functions in Google Sheets like =Average( ) and =Round( ) in Pyret but maybe I'm missing the point of the language.  I did a training with BootstrapWorld where Pyret is endoresed for its data analysis capabilities so I guess I was just expecting it to be a little more intuitive, but again I don't have a lot of coding experience.

Thank you for your help!

Shriram Krishnamurthi

unread,
Sep 12, 2023, 9:04:58 PM9/12/23
to pyret-...@googlegroups.com
Hi Timothy,

Happy to help.

For what you want to do, I think there's actually a very simple solution, which is the `mean` function that is built into the Data Science library.

I notice that for now, in all of your tables, the `time` column always has the value of 1, so the averages are going to, uninterestingly, also be 1. Nevertheless, here is how we obtain that value:

>>> mean(control-parachute-table, "time")
1

>>> mean(filtered-control-table, "time")
1

and so forth. We can even write a function to help us with this:

fun mean-time(t :: Table) -> Number:
  mean(t, "time")
end

So I can write:

>>> mean-time(control-parachute-table)
1

>>> mean-time(filtered-control-table)
1

Now, there are other ways to also have done this, but hopefully at least this lets you get off the ground (if you'll pardon the metaphor for a program about parachutes). I am of course happy to show you the other ways, too, once the above is clear.

Shriram

Timothy Malan

unread,
Sep 12, 2023, 11:17:52 PM9/12/23
to Pyret Discuss
Oh man, I was looking a the Pyret.org library and not the data science library.  There it is, doing exactly what I wanted the function to do.  I'm sorry about that Shriram.  Thank you!

Shriram Krishnamurthi

unread,
Sep 12, 2023, 11:19:00 PM9/12/23
to pyret-...@googlegroups.com
No apology needed, happy to help! Feel free to pop in with more questions!

Emmanuel Schanzer

unread,
Sep 13, 2023, 3:47:23 PM9/13/23
to Pyret Discuss
Don't forget, you can always find the complete contracts list in the back of the Data Science student workbook, or on the live contracts page! :)

Emmanuel

Reply all
Reply to author
Forward
0 new messages