PageSourceSearch

https://neu-pdi.github.io/cs2000-public-resources/assets/js/73a8032a.b8435467.js

js neu-pdi.github.io collected 2026-10-03 09:40:30 UTC 5,578 bytes, 1 lines download raw bytes

1"use strict";(self.webpackChunkinstructor_resources=self.webpackChunkinstructor_resources||[]).push([[770],{36913:(e,n,i)=>{i.r(n),i.d(n,{assets:()=>a,contentTitle:()=>c,default:()=>u,frontMatter:()=>l,metadata:()=>t,toc:()=>o});const t=JSON.parse('{"id":"6","title":"Practice 6 \u2014 for each","description":"Skills: 3","source":"@site/practice/6.md","sourceDirName":".","slug":"/6","permalink":"/cs2000-public-resources/practice/6","draft":false,"unlisted":false,"tags":[],"version":"current","sidebarPosition":6,"frontMatter":{"sidebar_position":6,"hide_table_of_contents":true,"title":"Practice 6 \u2014 for each"},"sidebar":"practiceSidebar","previous":{"title":"Practice 5 \u2014 Lists","permalink":"/cs2000-public-resources/practice/5"},"next":{"title":"Practice 7 \u2014 Structured and conditional data","permalink":"/cs2000-public-resources/practice/7"}}');var s=i(74848),r=i(28453);const l={sidebar_position:6,hide_table_of_contents:!0,title:"Practice 6 \u2014 for each"},c=void 0,a={},o=[{value:"Skills: 3",id:"skills-3",level:2},{value:"Reading: 5.3",id:"reading-53",level:2},{value:"Working with for each Loops",id:"working-with-for-each-loops",level:2},{value:"Implementing Basic Functions",id:"implementing-basic-functions",level:2},{value:"Product Function",id:"product-function",level:3},{value:"Length Function",id:"length-function",level:3},{value:"Finding Maximum Values",id:"finding-maximum-values",level:2},{value:"Maximum Function",id:"maximum-function",level:3},{value:"Wrap-Up",id:"wrap-up",level:2}];function d(e){const n={a:"a",code:"code",h2:"h2",h3:"h3",p:"p",pre:"pre",strong:"strong",...(0,r.R)(),...e.components};return(0,s.jsxs)(s.Fragment,{children:[(0,s.jsxs)(n.h2,{id:"skills-3",children:["Skills: ",(0,s.jsx)(n.a,{href:"/skills/#3",children:"3"})]}),"\n",(0,s.jsxs)(n.h2,{id:"reading-53",children:["Reading: ",(0,s.jsx)(n.a,{href:"https://dcic.pdi.run/recursive-data.html",children:"5.3"})]}),"\n",(0,s.jsx)(n.h2,{id:"working-with-for-each-loops",children:"Working with for each Loops"}),"\n",(0,s.jsxs)(n.p,{children:["To define our own list processing functions, we need a way to do computations for each element in the list. We'll use iteration with ",(0,s.jsx)(n.code,{children:"for each()"}),"."]}),"\n",(0,s.jsxs)(n.p,{children:["This requires being able to modify variables, which in Pyret means they must be declared with ",(0,s.jsx)(n.code,{children:"var"}),", and must be updated with the special operator ",(0,s.jsx)(n.code,{children:":="}),"."]}),"\n",(0,s.jsx)(n.p,{children:(0,s.jsx)(n.strong,{children:"Pattern is:"})}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-pyret",children:"var my-var = initial-value\nfor each(my-elt from my-list):\n    # CODE IMPLEMENTATION HERE\nend\nmy-var # Final result\n"})}),"\n",(0,s.jsxs)(n.p,{children:[(0,s.jsx)(n.strong,{children:"Example:"})," while ",(0,s.jsx)(n.code,{children:"sum"})," is built in, we could have defined it ourselves as:"]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-pyret",children:"fun my-sum(num-list :: List<Number>) -> Number block:\n    var total = 0\n    for each(n from num-list):\n        total := total + n\n    end\n    total\n    where:\n    my-sum([list: 0, 1, 2, 3]) is 6\nend\n"})}),"\n",(0,s.jsx)(n.h2,{id:"implementing-basic-functions",children:"Implementing Basic Functions"}),"\n",(0,s.jsx)(n.h3,{id:"product-function",children:"Product Function"}),"\n",(0,s.jsxs)(n.p,{children:["Define your own ",(0,s.jsx)(n.code,{children:"product"})," function that takes a list of numbers and returns their product (multiply all of them together)."]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-pyret",children:"fun my-product(num-list :: List<Number>) -> Number block:\n    # YOUR CODE HERE\nwhere:\n    my-product([list: 2, 3, 4]) is 24\n    my-product([list: 1, 2, 3, 4, 5]) is 120\nend\n"})}),"\n",(0,s.jsx)(n.h3,{id:"length-function",children:"Length Function"}),"\n",(0,s.jsxs)(n.p,{children:["Define a function ",(0,s.jsx)(n.code,{children:"my-length"})," that takes a list of any value and returns the number of elements in the list."]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-pyret",children:'fun my-length(l :: List) -> Number block:\n    # YOUR CODE HERE\nwhere:\n    my-length([list: "a", "b", "c"]) is 3\n    my-length([list: 1, 2, 3, 4, 5]) is 5\n    my-length([list: ]) is 0\nend\n'})}),"\n",(0,s.jsx)(n.h2,{id:"finding-maximum-values",children:"Finding Maximum Values"}),"\n",(0,s.jsx)(n.h3,{id:"maximum-function",children:"Maximum Function"}),"\n",(0,s.jsxs)(n.p,{children:["Define a function ",(0,s.jsx)(n.code,{children:"my-max"})," that finds the largest number in a list."]}),"\n",(0,s.jsx)(n.pre,{children:(0,s.jsx)(n.code,{className:"language-pyret",children:"fun my-max(num-list :: List<Number>) -> Number block:\n    # YOUR CODE HERE\nwhere:\n    my-max([list: 3, 7, 2, 9, 1]) is 9\n    my-max([list: 5]) is 5\n    my-max([list: -10, -5, -20]) is -5\nend\n"})}),"\n",(0,s.jsxs)(n.p,{children:["Is there any issue with the choice of ",(0,s.jsx)(n.code,{children:"initial-value"})," for this problem?"]}),"\n",(0,s.jsx)(n.h2,{id:"wrap-up",children:"Wrap-Up"}),"\n",(0,s.jsxs)(n.p,{children:["What's the difference between ",(0,s.jsx)(n.code,{children:"="})," and ",(0,s.jsx)(n.code,{children:":="})," in Pyret? When might you implement your own list functions instead of using built-ins?"]})]})}function u(e={}){const{wrapper:n}={...(0,r.R)(),...e.components};return n?(0,s.jsx)(n,{...e,children:(0,s.jsx)(d,{...e})}):d(e)}}}]);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.