Seven weeks seven language--erlang the second day of study

Source: Internet
Author: User

Key-value pairs

Title: 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 arguments, and returns the value corresponding to that key.

Get_value (map, key),  element (2, HD (Lists:dropwhile ({k, _}), Key/= K  End, Map) + + [{[],[]}]). List stitching

Lists:dropwhile takes away the element that does not conform to the given key, and then takes the first element of the list, which is the key-value pair to find. Note that the corresponding key can not be processed, the above code at the end of the list plus empty tuple, reduce the judgment.

Shopping list

Title: Consider a form such as [{Item quantity price}, ...] List of shopping. Write a list to parse, build the form like [{Item Total_price}, ...] List of items, where Total_price is quantity multiplied by price

Compute_price (shopinglist)  , [{Item, Quantity * Price} | | {Item, Quantity, price} <-shopinglist].


Simple list parsing. | | Syntax is equivalent to Lists:map.

Tic-Tac-Chess

Title: read a list or tuple of size 9, which represents the chess board of Tic-Tac-Tic-Tac-Toe. If the outcome is determined, the winner (x or O) is returned, and if there is no more chess to go, return to cat; if both sides have not won, return to No_winner.

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 (B Oard),   fun     Checkdealboard ([[[X, X, X] | _], _), X;     Checkdealboard ([O, O, O] | _], _), O;     Checkdealboard ([], true), No_winner;     Checkdealboard ([], false), cat;     Checkdealboard ([Hd | Tail], Canwin), and       if using a sentinel, you cannot invoke a function in it to avoid side effects       if         Canwin-Checkdealboard (Tail, true); anonymous function Recursive         true-checkdealboard (Tail, not Lists:any (fun (x), true; (o)->true; (_)->false end, Hd))       end  End (Dealboard, false).


The input is a two-dimensional list of 3*3, first processing the list, stitching a transpose board at the end of the list, and then stitching together diagonal lines. At this time the processed list contains 8 groups of elements, in turn recursive check, note for chess can walk judgment, if there is a row of a column of a diagonal only one piece, think there is chess to go, with Canwin to record whether there is chess to go.

Seven weeks seven language--erlang the second day of study

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.