These differences generally make python a slightly

This preview shows 6 out of 8 pages.

These differences generally make Python a slightly less fast language, but possibly an easier one to write programs for. Arrays in C, for example, have neither of these properties, but are much faster to use. You can use the Python array module to get the speed and space efficiency, if you need it. (You won’t need it in this course, however.) 7.22 3.2 Hierarchical lists Hierarchical lists You can have lists of lists, as in Scheme: >> L = [ ’a hierarchical’ ,[ ’list’ , ’can be useful’ ]] >> L [1] [’list’, ’can be useful’] >> L [1][0] ’list’ >> L [1][0][1] ’i’ These can be used for the same things we used them for in Scheme: To represent lists of objects that are themselves lists. To create more complicated structures. 7.23 Lists can represent structures We used structures in Scheme to group common data together into a single unit. We’ll see a similar construction in Python soon ( classes ). But it’s worth noticing that we can make structures just using lists. Example 2 . Here’s an example, to simulate posn structures. We’ll represent a posn structure as a two-element list. The first element of the list will be the x coordinate. The second element of the list will be the y coordinate. We just have to write a few functions for the constructors and the selectors for the posn type. 7.24 Implementing posn with lists ## a posn is a list [x,y] where each of x and y ## are of type (union float int) ## make_posn: (union float int) (union float int) posn def make_posn ( x_coord , y_coord ): return ([ x_coord , y_coord ]) Here, we return the two-element list. In the contract, we choose the “most general” possible, since integers and floating point numbers are treated differently in Python. 7.25 6
Image of page 6

Subscribe to view the full document.

And the selectors ## posn_x: posn (union float int) def posn_x ( p ): return ( p [0]) ## posn_y: posn (union float int) def posn_y ( p ): return ( p [1]) These are fairly straightforward. What is not as easy is to make a function that tests if a two-element list is supposed to be representing a posn . You can probably think about good ways to represent the data to include that information. Also, if it’s not been obvious yet, you can’t have the minus symbol in keywords in Python! Scheme is unusual in this respect, actually. 7.26 3.3 Mutation and lists Mutation and lists One important thing about lists is that they can be changed: >> L = [1,2, ’abc’ ] >> L [1] = 3 >> print L [1, 3, ’abc’] The command L [1] = 3 mutates the list. The second element (index 1) is replaced with the value 3 , instead of what was there previously. We did not see how to mutate the elements of lists in Scheme, but it’s fairly common in Python, and you will need to know how. 7.27 Mutation of lists makes serious trouble! In the last module, we talked about mutation in Scheme, and about the problem of aliasing.
Image of page 7
Image of page 8
You've reached the end of this preview.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern