brew install erlang
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"
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]