Created
February 12, 2014 07:04
-
-
Save tiensonqin/8951154 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
%% erlang pattern matching and list comprehension vs clojure destructure and hof | |
-module(shop). | |
-export([total/1]). | |
cost(oranges) -> 5; | |
cost(newspaper) -> 8; | |
cost(apples) -> 2; | |
cost(pears) -> 9; | |
cost(milk) -> 7. | |
sum([H | T]) -> H + sum(T); | |
sum([]) -> 0. | |
total(Items) -> | |
sum([cost(A) * B || {A, B} <- Items]). | |
(def m {:orange {:quantity 4, :price 3}, :apple {:quantity 3, :price 2}}) | |
(letfn [(per-count [item] | |
(let [{:keys [quantity price]} (val item)] | |
(* quantity price)))] | |
(reduce + (map per-count m))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment