CS52 - Fall 2015 - Class 1

Example code in this lecture


Lecture notes

  • PERM list, etc.

  • CS51, CS52 and CS62: how it all fits together
       - Who should be here?
          - taken CS51
          - taken AP CS
          - something similar and have gotten approval from a CS faculty member

  • course overview
       "The goal of this course is to give you a broad view of Computer Science, including the topics of algorithms and complexity, computer architecture and organization, programming languages with a solid understanding of functional programming and recursion, and finite automata and computability."

  • Course web page: http://www.cs.pomona.edu/classes/cs52/
       - Use Piazza (I'll have you all registered by the end of the day)
       - Mentor sessions
          - Use them to get help *if* you get stuck
          - These are NOT required
          - Work before coming to the session
          - The earlier sessions will be *much* less crowded and you'll get better support
       - Course readings
          - No official book, but many you can use to supplement (You should find one that you like)
          - Won't be extensive reading, but do read the handouts as I suggest
       - Course schedule

  • Administrative: http://www.cs.pomona.edu/classes/cs52/administrivia.html

  • Academic honesty and collaboration
       - I take it very seriously!
       - Read the administrative handout
       - Read the CS department policy
       - Understand what is appropriate and what is not!
       - Some things to watch out for:
          - Don't procrastinate!
             - One of the common ways I've seen people get into trouble is by not leaving enough time to work on the assignment.
             - If you find yourself in this situation, just turn in what you have and take it from there.
          - Do not give your work to anyone!
             - It doesn't help them.
             - It doesn't help you (and can get you into trouble).
          - Understand what type of collaboration is ok and what type is not.
             - You MAY:
                - Use/copy ANY of the examples covered in class or in the readings (in fact, you're encouraged to!)
                - Look at and work through examples from class with a partner or group
                - Talk about issues raised in the reading with a partner or group
                - Talk at a high-level (i.e. NO CODING) about the assignments with a partner or group
                - Help someone with a syntax problem (i.e. code won't compile)... this is the only time you should look at someone else's code
                - Help someone understand an error message
             - You may NOT:
                - look at anyone else's code to help yourself
                - give or show your code to anyone else (except to help diagnose a syntax problem)
                - search online for answers, solutions, etc.
                - sit next to someone in the class that you are talking with about the assignment (ideally, just don't sit next to anyone who's in the class)

  • Assignment 0
       - Posted and due Friday at 5pm
       - If you've never taken a CS class, you won't have an account yet. Wait until Wednesday to start the assignment (though you can look at it now)

  • Two ways of interacting with SML
       1) the interpreter
          - Type commands and get back results
          - Nice for testing things out and doing simple things
          - I'll use this a lot in class
       2) by editing a file and then running in the interpreter
          - Much more useful when writing actual programs
          - Same as if you'd typed the commands into the interpreter

  • To start SML you first open Terminal, which is a shell
       - What can you do in the shell?
          - almost everything you can do with the GUI (i.e. the windows environment)
          - navigate the file system
          - edit files
          - run programs
          - ...
       - then just type sml and you'll see:
          Standard ML of New Jersey v110.69 [built: Mon Jun 8 23:24:21 2009]

          The dash is the statement prompt from SML

  • Your first SML function
       > fun add1 x = x + 1;
       val add1 = fn : int -> int

       - The syntax for declaring a function is:
          fun <name_of_function> <arguments> = <function_body>;

          - it looks a bit more like declaring a mathematical function
       - What do you think: "val add1 = fn : int -> int" is?
          - This is the response from SML saying that it has defined a new value
             - The responses can always be broken down as follows:

                val <variable_name> = <value> : <type>
          - The name of the value is add1
          - fn tells us it's a function
          - int -> int is the type of the function
       - This already tells us some interesting things about SML:
          - functions are "first-class entities" in SML. They are treated like any other *value* in the language (ints, strings, etc.)
             - they can stored to variables
             - they can be passed as arguments to other functions
             - they can be returned from functions
          - int -> int
             - this says that the function takes an integer as a parameter and returns an integer
             - How did SML know this?
                - Everything in SML has a type
                   - this is called a "strongly-typed" language
                   - values do
                   - functions, therefore, also do
                   - operators (like +, -, *, etc.) do
                - SML has a very good type inferring system
                   - + we know is used on numbers
                   - therefore, x must be a number
                   - therefore the function both takes and returns a number
  • Using a function
       - Once we've declared a function, we can use it
       > add1 10;
       val it = 11 : int

       - As before, the response from SML can be understood:
          - name of the variable: it
             - what do you think "it" is?
                - name for the previous statement when we don't assign it to a value
                   - can actually use it if you'd like in the next statement
          - value is 11
          - type of the value is int
          - get used to looking at the response and understanding it!

       - Anything interesting about how we called the function?
          - no parentheses!
             - we could have added them, but for style reasons we won't put them in
          - like java, we will terminate all statements with a semi-colon

  • The power function
       - We'd like to write a function called power that takes two numbers, n and k, and produces n^k
       - Write this in Java:
          public static int power(int n, int k){
             if( n == 0 ){
                return 1
                n * power(n, k-1)
       - look at the power function in power.sml code
          - now I'm using Aquamacs (an Emacs editor) to view the file
          - like before, we use fun to define a new function
          - what's different?
             - we're not using if/else like in Java
          - SML uses "patterns" to

  • Using the function
       - We can type this (or copy and paste this) into the SML window and we get the following response:
          val power = fn : int * int -> int

       - Anything new here?
          - the input type to the function is "int * int"
             - this is called a "tuple"
                - a tuple is a combination of two or more values
                - they are defined by putting comma separated values inside parentheses
                   > (1, 2);
                   val it = (1,2) : int * int
             - the function only has one input
                - in fact, all functions in SML only have one input!
       - to call it we give it a tuple:
          > power (10, 2);
          val it = 100 : int

  • Any problems with our function?
       - Doesn't handle negative numbers
       - How could we fix this?

  • Look at the power2 function in power.sml code
       - We now have three patterns in the function
          1) for when k = 0
          2) for when n = 0 (strictly, this could be handled by the third case)
          3) the general case

       - the if-then-else statement is used to handle the case where k < 0
          - notice that if-then-else represents a value
             - each branch has a value of the same type
             - no matter what branch we go down, we obtain a value
             - we'll see this type of behavior A LOT