Created
November 23, 2011 21:39
-
-
Save japaz/1390002 to your computer and use it in GitHub Desktop.
7L7W Erlang - Day2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% Consider a shopping list that looks like [{item quantity price}, ...]. | |
% Write a list comprehension that builds a list of items of the form | |
% [{item total_price}, ...], where total_price is quantity times price. | |
-module(comprehesion). | |
ShoppingList = [{apple, 4, 0.2}, {orange, 3, 1.1}, {peach, 4, 1}]. | |
Total = [{Product, Quantity * Price} || {Product, Quantity, Price} <- ShoppingList]. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% Consider a list of keyword-value tuples, such as [{erlang, "a functional | |
% language"}, {ruby, "an OO language"}]. Write a function that accepts | |
% the list and a keyword and returns the associated value for | |
% the keyword. | |
-module(map). | |
-export([get/2]). | |
get([], _) -> ""; | |
get([{Key, Value}|_], Key) -> Value; | |
get([_|Tail], Key) -> get(Tail, Key). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment