CSC302 2011S, Class 15: Erlang (1) Overview: * Java vs. Scala. * Your Erlang questions. * Lab. Admin: * Celebration of MathCounts! teams performance * Don't forget that Assignment 5 is due Saturday night. I know that there are some questions. * I plan to post the next assignment on Saturday night. * No office hours today: Another CSFS meeting. * Reminder: Summer research applications are due today. * EC for CS table today: CUDA and GPUs. * EC for track meet this weekend. Watch AH run and jump. Figure out when. Why might Java be better than Scala? * It might not. Write a short paragraph on why not. * No overloading in Scala. Whoops. Solved. * Reflection. * Array syntax. Lame. No familiarity issues allowed. * What happens if two mixins define the same function? What if they're not my mixins or class? * Resume' word * The Perl issue: Different programmers use different language features, making code harder to read/maintain/whatever. Erlang: Bits of Prolog in a Functional System * To understand Erlang, you need to think about * Key features of Prolog * Pure functional programming (and why) * Parallelism Key feature of prolog: Pattern matching * Given two values, try to match them together [X,X] vs. [2,2]. => X is 2 [X,X] vs. [2,3]. => NO. * Matching was often implicit. goal is foo([X,X]). rule is foo([2,3]). * Erlang makes the matching explicit. EXP1 = EXP2 means "try to match the two things" * In Prolog, matching gives assignment of variables (and yes), * In Erlang, matching gives the matched value. * Erlang matching does not like unbound variables on the right. * When Erlang can't match, it throws an exception. > X1 = blue. blue > X1. blue > X2 = blue. blue > X1 = X2. blue > X3 = red. red > X1 = X3. Boom! Functional part: "Variables" don't vary. * Three mechanisms in Scheme (define name val) (let ((name val)) ...) ((lambda (name) ...) val) * They're really constants. Why not let variables vary? Parallelism! -- Starting erlang $ erl Loading a file 1> c(prefix). Asking it to evaluate 2> 3 + 4. Asking it to match 3> X = [1,2]. Sequencing 4> X = 1, X + 2.