Seven weeks and seven languages -- Erlang's second day and seven weeks of erlang's self-study

Source: Internet
Author: User

Seven weeks and seven languages -- Erlang's second day and seven weeks of erlang's self-study
Key-value pairs

Question: consider a list of key-value tuples, such as [{erlang, "a functinal language"}, {ruby, "an OO language"}]. Write a function that accepts the list and key as parameters and returns the value corresponding to the key.

Get_value (Map, Key)-> element (2, hd (lists: dropwhile (fun ({K, _})-> Key/= K end, Map) ++ [{[], []}]). % ++ Concatenates the list

Lists: dropwhile removes the elements that do not match the given Key, and then obtains the first element of the list, that is, the Key-value pair to be searched. Note that the corresponding Key cannot be found. The above Code adds an empty tuples at the end of the list to reduce the judgment.

Shopping List

Example: [{item quantity price},…] . Write a list parsing statement to build a table like [{item total_price},…] In the product list, where total_price is quantity multiplied by price

compute_price(ShopingList) ->  [{Item, Quantity * Price} || {Item, Quantity, Price} <- ShopingList].


Simple list parsing. | The syntax is equivalent to lists: map.

Jing Ziqi

Question: read a list or tuples of 9, which indicates the board of tic-tac-toe. If the winner is determined, the winner (x or o) is returned. If no other player can play, cat is returned. If neither party wins, no_winner is returned.

Tictactoe (Board)-> DealBoard = Board ++ fun ([[A1, B1, C1], [A2, B2, C2], [A3, B3, C3]) -> [[A1, A2, A3], [B1, B2, B3], [C1, C2, C3] end (Board) ++ fun ([A1, _, C1], [_, B2, _], [A3, _, C3])-> [[A1, B2, C3], [A3, B2, c1] end (Board), fun CheckDealBoard ([[x, x, x] | _], _)-> x; CheckDealBoard ([o, o, o] | _], _)-> o; CheckDealBoard ([], true)-> no_winner; CheckDealBoard ([], false)-> cat; checkDealBoard ([Hd | Tail], CanWin)-> % if uses a Sentel, where the function cannot be called to avoid side effects if CanWin-> CheckDealBoard (Tail, true ); % anonymous function recursion true-> CheckDealBoard (Tail, not lists: any (fun (x)-> true; (o)-> true; (_)-> false end, hd) end (DealBoard, false ).


The input is a 3*3 two-dimensional list. First, process the list, splice a transpose Board at the end of the list, and then splice two sets of diagonal lines. At this time, the processed list contains eight groups of elements, which are checked recursively. Pay attention to the determination of whether there are any chess moves. If there is only one chess piece in a column of a Certain Row, it is considered that there is a chess move, use CanWin to record whether there is any move.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.