Skip to content

Instantly share code, notes, and snippets.

@prayagupa
Last active December 26, 2017 00:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prayagupa/ee6e94a89c2874398ecee696011b3278 to your computer and use it in GitHub Desktop.
Save prayagupa/ee6e94a89c2874398ecee696011b3278 to your computer and use it in GitHub Desktop.
learnyousomeerlang, erlang

installation

brew install erlang

Tuple datastructure

Tuples are datastructure to store sequence of multiple data which could be of different types.

lets define inventoty as sequence of Item, Qty, UnitPrice

27> ShirtInventory = {"shirts", 100, 88}.
{"shirts",100,88}

ifdef(comment).
   pattern match
-endif.
28> {Item, Qty, UnitPrice} = ShirtInventory.
{"shirts",100,88}
29> Item.
"shirts"

List datastructure

a sequence of data of same/different types, unlike tuples can have unknown number of elements in it. has head and tail.

7> [1, 3, 5, 7] ++ [11, 13, 17, 19].
[1,3,5,7,11,13,17,19]
1> FibList = [1, 1, 2, 3, 5].
[1,1,2,3,5]

2> AnotherFibList = [0 | FibList].
[0,1,1,2,3,5]

#list pattern match

4> [Head|Tail] = AnotherFibList.
[0,1,1,2,3,5]
522> Head.
0
5> Tail.                             
[1,1,2,3,5]

6> [H | T] = Tail.
[1,1,2,3,5]
25> H.
1
7> T.
[1,2,3,5]

List comprehension - ways to build or modify lists.

[0.85*Price || Price <- [100,200,300,400]].

## get discounted prices for items in cart

2> Cart = [{"nexus", 100},{"woofer", 200},{"mic", 300},{"Drum", 400}]. 
[{"nexus",100},{"woofer",200},{"mic",300},{"Drum",400}]

3> [0.85*Price || {Item, Price} <- Cart].
[85.0,170.0,255.0,340.0]

http://learnyousomeerlang.com/starting-out-for-real#numbers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment