1"use strict";(self.webpackChunkinstructor_resources=self.webpackChunkinstructor_resources||[]).push([[9831],{55729:(e,n,t)=>{t.r(n),t.d(n,{assets:()=>c,contentTitle:()=>d,default:()=>u,frontMatter:()=>s,metadata:()=>i,toc:()=>l});const i=JSON.parse('{"id":"7","title":"Practice 7 \u2014 Structured and conditional data","description":"Skills: 4","source":"@site/practice/7.md","sourceDirName":".","slug":"/7","permalink":"/cs2000-public-resources/practice/7","draft":false,"unlisted":false,"tags":[],"version":"current","sidebarPosition":7,"frontMatter":{"sidebar_position":7,"hide_table_of_contents":true,"title":"Practice 7 \u2014 Structured and conditional data"},"sidebar":"practiceSidebar","previous":{"title":"Practice 6 \u2014 for each","permalink":"/cs2000-public-resources/practice/6"},"next":{"title":"Practice 8 \u2014 Recursion on Lists","permalink":"/cs2000-public-resources/practice/8"}}');var r=t(74848),a=t(28453);const s={sidebar_position:7,hide_table_of_contents:!0,title:"Practice 7 \u2014 Structured and conditional data"},d=void 0,c={},l=[{value:"Skills: 4",id:"skills-4",level:2},{value:"Reading: 6.1, 6.2",id:"reading-61-62",level:2},{value:"Structured Data",id:"structured-data",level:2},{value:"Defining a Structured Data Type",id:"defining-a-structured-data-type",level:3},{value:"Creating Examples",id:"creating-examples",level:3},{value:"Extracting Fields",id:"extracting-fields",level:3},{value:"Writing Functions with Structured Data",id:"writing-functions-with-structured-data",level:3},{value:"Conditional Data",id:"conditional-data",level:2},{value:"Defining a Conditional Data Type",id:"defining-a-conditional-data-type",level:3},{value:"Creating Examples",id:"creating-examples-1",level:3},{value:"Taking Apart Variants with <code>cases</code>",id:"taking-apart-variants-with-cases",level:3},{value:"Processing Fields in Variants",id:"processing-fields-in-variants",level:3},{value:"Wrap-Up",id:"wrap-up",level:2}];function o(e){const n={a:"a",code:"code",h2:"h2",h3:"h3",p:"p",pre:"pre",...(0,a.R)(),...e.components};return(0,r.jsxs)(r.Fragment,{children:[(0,r.jsxs)(n.h2,{id:"skills-4",children:["Skills: ",(0,r.jsx)(n.a,{href:"/skills/#4",children:"4"})]}),"\n",(0,r.jsxs)(n.h2,{id:"reading-61-62",children:["Reading: ",(0,r.jsx)(n.a,{href:"https://dcic.pdi.run/intro-struct-data.html",children:"6.1"}),", ",(0,r.jsx)(n.a,{href:"https://dcic.pdi.run/Collections_of_Structured_Data.html",children:"6.2"})]}),"\n",(0,r.jsx)(n.h2,{id:"structured-data",children:"Structured Data"}),"\n",(0,r.jsx)(n.p,{children:"Many pieces of real-world information consist of several parts that should be kept together. Structured data bundles multiple fields into a single value."}),"\n",(0,r.jsx)(n.h3,{id:"defining-a-structured-data-type",children:"Defining a Structured Data Type"}),"\n",(0,r.jsxs)(n.p,{children:["Define a new data type called ",(0,r.jsx)(n.code,{children:"Student"})," to represent a student:"]}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-pyret",children:"data Student:\n | student(name :: String, id :: Number, gpa :: Number)\nend\n"})}),"\n",(0,r.jsx)(n.h3,{id:"creating-examples",children:"Creating Examples"}),"\n",(0,r.jsxs)(n.p,{children:["Create instances of ",(0,r.jsx)(n.code,{children:"Student"})," using the constructor ",(0,r.jsx)(n.code,{children:"student"}),":"]}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-pyret",children:'al
1ice = student("Alice Smith", 12345, 3.7)\nbob = student("Bob Martinez", 23456, 3.2)\ncarol = student("Carol Johnson", 34567, 3.9)\n'})}),"\n",(0,r.jsx)(n.p,{children:"What do you expect to happen if you try to create a student with a string for the ID number?"}),"\n",(0,r.jsx)(n.h3,{id:"extracting-fields",children:"Extracting Fields"}),"\n",(0,r.jsxs)(n.p,{children:["Design a function ",(0,r.jsx)(n.code,{children:"student-info"})," that returns a formatted string for a student:"]}),"\n",(0,r.jsxs)(n.p,{children:["What is the result of ",(0,r.jsx)(n.code,{children:"student-info(alice)"}),"?"]}),"\n",(0,r.jsx)(n.h3,{id:"writing-functions-with-structured-data",children:"Writing Functions with Structured Data"}),"\n",(0,r.jsxs)(n.p,{children:["Design a function ",(0,r.jsx)(n.code,{children:"honors-student"})," that returns true if a student has a GPA of 3.5 or higher:"]}),"\n",(0,r.jsx)(n.h2,{id:"conditional-data",children:"Conditional Data"}),"\n",(0,r.jsx)(n.p,{children:"Sometimes data can come in multiple forms and we need a way to represent alternatives within one type. Conditional data can be one of several variants."}),"\n",(0,r.jsx)(n.h3,{id:"defining-a-conditional-data-type",children:"Defining a Conditional Data Type"}),"\n",(0,r.jsxs)(n.p,{children:["Define a new type called ",(0,r.jsx)(n.code,{children:"OrderType"})," with three variants:"]}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-pyret",children:"data OrderType:\n | pickup\n | delivery(address :: String, tip :: Number)\n | curbside(car-color :: String, license-plate :: String)\nend\n"})}),"\n",(0,r.jsx)(n.p,{children:"How many different ways can someone receive their order according to our OrderType?"}),"\n",(0,r.jsx)(n.h3,{id:"creating-examples-1",children:"Creating Examples"}),"\n",(0,r.jsxs)(n.p,{children:["Create instances of ",(0,r.jsx)(n.code,{children:"OrderType"}),":"]}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-pyret",children:'order-1 = pickup\norder-2 = delivery("360 Huntington Ave, Boston MA", 5.00)\norder-3 = curbside("red", "ABC123")\n'})}),"\n",(0,r.jsxs)(n.h3,{id:"taking-apart-variants-with-cases",children:["Taking Apart Variants with ",(0,r.jsx)(n.code,{children:"cases"})]}),"\n",(0,r.jsxs)(n.p,{children:["Design a function ",(0,r.jsx)(n.code,{children:"order-instructions"})," that returns instructions for fulfilling an order:"]}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-pyret",children:'fun order-instructions(o :: OrderType) -> String:\n cases (OrderType) o:\n | pickup => # YOUR CODE HERE\n | delivery(addr, tip-amount) => # YOUR CODE HERE\n | curbside(color, plate) => # YOUR CODE HERE\n end\nwhere:\n order-instructions(pickup) is "Order ready for pickup at counter"\n order-instructions(order-3) is "Look for red car with plate ABC123 in parking lot"\nend\n'})}),"\n",(0,r.jsx)(n.h3,{id:"processing-fields-in-variants",children:"Processing Fields in Variants"}),"\n",(0,r.jsxs)(n.p,{children:["Design a function ",(0,r.jsx)(n.code,{children:"total-cost"})," that calculates the total cost including any service fees:"]}),"\n",(0,r.jsx)(n.p,{children:"What information do we use and ignore in each case?"}),"\n",(0,r.jsx)(n.h2,{id:"wrap-up",children:"Wrap-Up"}),"\n",(0,r.jsxs)(n.p,{children:["When would you use structured data instead of separate variables? Why is pattern matching with ",(0,r.jsx)(n.code,{children:"cases"})," better than using if-statements for conditional data?"]})]})}function u(e={}){const{wrapper:n}={...(0,a.R)(),...e.components};return n?(0,r.jsx)(n,{...e,children:(0,r.jsx)(o,{...e})}):o(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.