CS52 - Spring 2016 - Class 1

Example code in this lecture

   power.sml

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: 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 while working 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 be 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 have a type
                   - functions, therefore, also have a type
                   - operators (like +, -, *, etc.) have a type as well
                - 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 broken down:
          - 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, recursively:
          public static int power(int n, int k){
             if( n == 0 ){
                return 1
             }else{
                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 match different input cases
             - it can do many things, like match constants (e.g. 0), but not everything (e.g. match all negative numbers)
             - pattern matching starts at the top and works down. When it finds one that matches, it executes that code and only that code
                - for example, the order in the power function *does* matter. If we'd put (n,k) first, the (n,0) case would never be reached

  • 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