OJ Reeves. Agenda  The Boring Stuff Basic syntax, expressions, funcs, records Atoms, BIFs, data types, bools, equality  The not so Boring Stuff Pattern.

Slides:



Advertisements
Similar presentations
Practical Erlang Programming Process Error Handling.
Advertisements

Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Chapter 4 Computation Bjarne Stroustrup
Chapter 4 - Control Statements
LIS651 lecture 1 PHP basics, database introduction Thomas Krichel
Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same.
ThinkPython Ch. 10 CS104 Students o CS104 n Prof. Norman.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  Elements may appear more than once.
A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
ML Exceptions.1 Standard ML Exceptions. ML Exceptions.2 Exceptions – The Need  An extensive part of the code is error handling  A function can return.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  ML lists are immutable.  Elements.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
Thomas Arts Industrial Use of a Functional Language Thomas Arts Ericsson Computer Science Laboratory Stockholm, Sweden
Erlang Paul Ang, Alex Quintero, Jessica Lombardi, Robin Adams.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Introduction to ML – Part 1 Frances Spalding. Assignment 1 chive/fall05/cos441/assignments/a1.ht m
CS16 Week 2 Part 2 Kyle Dewey. Overview Type coercion and casting More on assignment Pre/post increment/decrement scanf Constants Math library Errors.
Introduction to ML – Part 1 Kenny Zhu. Assignment 2 chive/fall07/cos441/assignments/a2.ht m
Lecture 3: Topics If-then-else Operator precedence While loops Static methods Recursion.
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
Introduction to ML Last time: Basics: integers, Booleans, tuples,... simple functions introduction to data types This time, we continue writing an evaluator.
Introduction to ML You will be responsible for learning ML on your own. Today I will cover some basics Read Robert Harper’s notes on “an introduction to.
Lecture 1 The Basics (Review of Familiar Topics).
Control Structures: Getting Started Sequence and Selection also arithmetic operators, data types, logical operators.
CIS Computer Programming Logic
17-Sep-15 Erlang. Running Erlang Double-click on the Erlang icon, or type erl into a terminal (cmd) window You can try short pieces of code from here,
Introduction to Python
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
Programming Language Concepts (CIS 635) Elsa L Gunter 4303 GITC NJIT,
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Agenda Exam #1 Review Modulus Conditionals Boolean Algebra Reading: Chapter Homework #5.
Shell Scripting AFNOG IX Rabat, Morocco May 2008.
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
Overview of the Haskell 98 Programming Language
Practical Erlang Programming Basic Erlang. © Erlang Training and Consulting Ltd2Basic Erlang Practical Erlang Programming.
Xin Liu Feb 4, * Use midterm questions for previous 231/217 midterms for practices * ams
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
Erlang Noah Dietrich Chris Ruffalo.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Erlang Erricsson and Ellemtel Computer Science Laboratories
List Operations CSCE 314 Spring CSCE 314 – Programming Studio Tuple and List Patterns Pattern matching with wildcards for tuples fst (a, _) = a.
Today’s Agenda ML Development Workflow –Emacs –Using use –The REPL More ML –Shadowing Variables –Debugging Tips –Boolean Operations –Comparison Operations.
1 Objective Caml (Ocaml) Aaron Bloomfield CS 415 Fall 2005.
Message Passing, Concurrency, And Parallelism with Erlang Presented By: Craig R. Kuehn Department of Computer Science and Software Engineering University.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Erlang - a complete development environment for concurrent programming RTLab. Kim Tae-Hyon.
Basic concepts of C++ Presented by Prof. Satyajit De
Principles of programming languages 12: Functional programming
Types CSCE 314 Spring 2016.
CSE 341 Section 1 (9/28) With thanks to Dan Grossman, et. al. from previous versions of these slides.
ML: a quasi-functional language with strong typing
CSE 341 Section 1 (9/28) With thanks to Dan Grossman, et. al. from previous versions of these slides.
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
CS 1428 Exam I Review.
User input We’ve seen how to use the standard output buffer
Nicholas Shahan Spring 2016
CSE 341: Programming Languages Section 1
Erlang 15-Nov-18.
CSE 341: Programming Langs
CSE341: Programming Languages Section 1
Philip Ira Alan Maynard
CSE 341 Section 1 Nick Mooney Spring 2017
CSE341: Programming Languages Section 1
CSE 341 Section 5 Winter 2018.
If Statements.
CSE 341 Section 2 Nick Mooney Spring 2017
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
General Computer Science for Engineers CISC 106 Lecture 03
CS 1428 Exam I Review.
CSE341: Programming Languages Section 1
Presentation transcript:

OJ Reeves

Agenda  The Boring Stuff Basic syntax, expressions, funcs, records Atoms, BIFs, data types, bools, equality  The not so Boring Stuff Pattern matching, guards, binaries  Some Interesting Stuff processes, concurrency  Some Cool Stuff Distribution, fault-tolerance  A Problem  OTP (depending on time)

Play Along  Load werl or erl, that’s your REPL.  Modules? Create one for mucking around, foo.erl:  -module(foo). % matches file -compile(export_all). % your stuff goes here  Compile file in REPL: c(foo). Your code is auto-loaded when compiled.

The Boring Stuff  IQ = 10. % “binding” IQ = 10. % ok, same value bound IQ = 11. % runtime error  Name = “OJ”.  Response = >.  Point = {1.0, 3.2}. {X, Y} = Point. Tuples can be of mixed “type” / nested  random() -> 4. % ode to XKCD  [“I am”, “a list of”, “strings”]. Lists can be of mixed “type” [1, 10.4, “fear me!”, my_atom]

More Boring Stuff  clean(Text) -> Funs = [fun rem_bad/1, fun add_punc/1], lists:foldl(fun(X, T) -> X(T) end, Text, Funs).  -record(person, {name, age, state=sober}).  get_name(#person{name=Name}) -> Name.  set_name(Person, Name) -> Person#person{name=Name}.  Common: ok vs {error, “Reason”}  ok = some_module:do_something().

The Last of the Boring Stuff  Status = dead. anything_like_this, ‘OR THIS’  BIF – “Built in Function” integer_to_list(), list_to_binary(), atom_to_list(), etc.  Data types – don’t really have any!  Boolean stuff and, or, xor, andalso, orelse  Equality =:= =/= (basically == and !=) == /= (when mixed “types” such as 1 == 1.0) =, >, etc (=< is not a typo!)

The Not So Boring Stuff  div(N, 0) -> infinity; % cont.. div(N, D) -> N /D.  first_item_even([H|T]) -> H rem 2 =:= 0.  get_status(Person) -> case P#person.name of ○ “OJ” -> loser; % continue _ -> legend % no, or. end. % terminated here

More Not So Boring Stuff  is_decrepit(#person{age=Age}) when Age > 80 -> true;  is_decrepit(_Person) -> false.  Pattern matching is binding! rock, paper, scissors (no lizard/spock) Winner = case {Move1, Move2} of ○ {paper, rock} -> player1; {rock, scissors} -> player1; {scissors, paper} -> player1; {Move, Move} -> none; % oooh! _ -> player2 end.

More Not So Boring Stuff  Binaries are special lists >. >. % or int data  Can be pattern matched: A = >. > = A. % C == 2  Bit syntax – pull apart binaries!

Last of the Not So Boring Stuff  Pulling an MP3 header apart:  valid_header( case {B, C} of ○ {1, _} -> false; % bad version ○ {_, 0} -> false; % bad layer ○ _ -> true end;  valid_header(_) -> false. Thanks Joe!

Last of the Not So Boring Stuff  Pulling an MP3 header apart:  valid_header( case {B, C} of ○ {1, _} -> false; % bad version ○ {_, 0} -> false; % bad layer ○ _ -> true end;  valid_header(_) -> false.

Fizzbuzz  For each number from 1 to 100, output: “Fizz” if divisible by 3 “Buzz” if divisible by 5 “FizzBuzz” if divisible by both 3 and 5 The number itself otherwise.

Some Interesting Stuff  Processes – very cheap, easy to spin up. Not “native”. spawn(Fun) -> pid() ○ Spawn Fun/0 in a new process spawn(Node, Fun) -> pid() ○ Spawn Fun/0 on the given node spawn(Mod, Fun, Args) -> pid() ○ Spawn Mod:Fun(Args) in a new process spawn(Node, Mod, Fun, Args) -> pid() ○ See if you can guess…  Send messages to pids: Pid ! Message.

More Interesting Stuff  Demo of echo().  Parallel map: spawn() for each “job” Wait for response from each Pid Combine results Show me teh codez.

Agenda  The Boring Stuff Basic syntax, expressions, funcs, records Atoms, BIFs, data types, bools, equality  The not so Boring Stuff Pattern matching, guards, binaries  Some Interesting Stuff processes, concurrency  Some Cool Stuff Distribution, fault-tolerance  Fun Problem  Building a Webserver