[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"detail-sidebar-cat-0-en-105":3,"doc-seo-697979-105":59,"doc-detail-697979-en":130,"doc-similar-697979":161},{"code":4,"msg":5,"data":6},0,"success",[7,13,18,23,28,33,38,43,48,51,55],{"id":8,"doc_module":4,"doc_module_name":9,"category_name":10,"show_sort_weight":11,"slug":12},1,"Document","Story & Novel",90,"story-novel",{"id":14,"doc_module":4,"doc_module_name":9,"category_name":15,"show_sort_weight":16,"slug":17},2,"Literature",80,"literature",{"id":19,"doc_module":4,"doc_module_name":9,"category_name":20,"show_sort_weight":21,"slug":22},4,"Exam",70,"exam",{"id":24,"doc_module":4,"doc_module_name":9,"category_name":25,"show_sort_weight":26,"slug":27},5,"Comic",60,"comic",{"id":29,"doc_module":4,"doc_module_name":9,"category_name":30,"show_sort_weight":31,"slug":32},6,"Technology",50,"technology",{"id":34,"doc_module":4,"doc_module_name":9,"category_name":35,"show_sort_weight":36,"slug":37},7,"Healthcare",40,"healthcare",{"id":39,"doc_module":4,"doc_module_name":9,"category_name":40,"show_sort_weight":41,"slug":42},8,"Research & Report",30,"research-report",{"id":44,"doc_module":4,"doc_module_name":9,"category_name":45,"show_sort_weight":46,"slug":47},9,"Religion & Spirituality",20,"religion-spirituality",{"id":46,"doc_module":4,"doc_module_name":9,"category_name":49,"show_sort_weight":46,"slug":50},"World Cup","world-cup",{"id":52,"doc_module":4,"doc_module_name":9,"category_name":53,"show_sort_weight":52,"slug":54},10,"Lifestyle","lifestyle",{"id":56,"doc_module":4,"doc_module_name":9,"category_name":57,"show_sort_weight":24,"slug":58},19,"General","general",{"code":4,"msg":60,"data":61},"ok",{"site_id":62,"language":63,"slug":64,"title":65,"keywords":66,"description":67,"schema_data":68,"social_meta":123,"head_meta":125,"extra_data":127,"updated_unix":129},105,"en","ocaml-introduction-tuples-and-lists","OCaml Introduction: Tuples and Lists","","OCaml Introduction: Tuples and Lists explains how OCaml represents compound values with tuples and lists, focusing on type flexibility and fixed vs. variable length. It clarifies tuple types as ordered sequences of n types using *, and shows how tuple values are constructed with parentheses and commas. The text demonstrates extracting tuple components via pattern matching, including the use of the dummy pattern _. It also introduces the unit type and sequencing with semicolons in function calls.",{"@graph":69,"@context":122},[70,84,105],{"@type":71,"itemListElement":72},"BreadcrumbList",[73,77,79,82],{"item":74,"name":75,"@type":76,"position":8},"https://docshare.wps.com","Home","ListItem",{"item":78,"name":9,"@type":76,"position":14},"https://docshare.wps.com/document/",{"item":80,"name":30,"@type":76,"position":81},"https://docshare.wps.com/document/technology/",3,{"item":83,"name":65,"@type":76,"position":19},"https://docshare.wps.com/document/ocaml-introduction-tuples-and-lists/697979/",{"url":83,"name":65,"@type":85,"image":86,"author":91,"headline":65,"publisher":94,"fileFormat":97,"inLanguage":63,"description":67,"dateModified":98,"datePublished":99,"encodingFormat":97,"isAccessibleForFree":100,"interactionStatistic":101},"DigitalDocument",{"url":87,"@type":88,"width":89,"height":90},"https://docshare.wps.com/thumbnails/ocaml-introduction-tuples-and-lists/697979.png","ImageObject",300,407,{"name":92,"@type":93},"Oliver","Person",{"url":74,"name":95,"@type":96},"DocShare","Organization","application/pdf","2026-11-16","2026-11-12",true,{"@type":102,"interactionType":103,"userInteractionCount":81},"InteractionCounter",{"@type":104},"ViewAction",{"@type":106,"mainEntity":107},"FAQPage",[108,114,118],{"name":109,"@type":110,"acceptedAnswer":111},"What is the main difference between tuples and lists in OCaml?","Question",{"text":112,"@type":113},"A tuple can store a fixed number of values of different types, while a list can store an unlimited number of values but all of the same type.","Answer",{"name":115,"@type":110,"acceptedAnswer":116},"How do you extract values from an OCaml tuple?",{"text":117,"@type":113},"Use pattern matching with a pattern like (name, age, _, sex), where OCaml binds names to the corresponding tuple components and _ ignores an unwanted value.",{"name":119,"@type":110,"acceptedAnswer":120},"Why does OCaml use the unit type and the value ()?",{"text":121,"@type":113},"The unit type represents exactly one element, () , conveying no information. It’s used when a function conceptually returns nothing, such as print_string and sequencing print operations.","https://schema.org",{"og:url":83,"og:type":124,"og:title":65,"og:site_name":95,"og:description":67},"article",{"robots":126,"canonical":83},"index,follow",{"doc_id":128,"site_id":62},697979,1794446560,{"code":4,"msg":5,"data":131},{"doc_id":128,"user_id":132,"nickname":92,"user_avatar":133,"doc_module":4,"category_id":29,"category_name":30,"doc_title":65,"doc_description":67,"doc_content":134,"file_id":135,"file_url":136,"file_type":137,"file_size":138,"view_count":81,"is_deleted":4,"is_public":8,"is_downloadable":8,"audit_status":8,"page_count":24,"language":139,"language_code":63,"site_id":62,"html_lang":63,"table_of_contents":140,"faqs":141,"seo_title":142,"seo_description":67,"update_tm":129,"read_time":143,"preview_page_count":19,"preview_urls":144},8796095461610,"https://ap-avatar.wpscdn.com/davatar_276721f389ce27ea32af1340a28f341c","OCaml Introduction: Tuples and Lists  \nJeﬀ Meister  \nCSE 130, Winter 2011  \nSo far, we have only dealt with expressions of single values of a single type, like 5 : int or 9 .7 : float or \"cse130\" : string (note that an OCaml string is not made of multiple chars, but has its own built-in type) . There are plenty of combinations left to consider. What about compound values? The language has a few built-in constructs for these, including tuples and lists. The simple overview: a tuple can hold values of diﬀerent types, but only a ﬁxed number of them; a list can hold an unlimited number of values, but only of the same type.  \nYou might well ask, what about holding an unlimited number of values of diﬀerent types? I would reply that such a collection is nearly useless. Because every function has exactly one input type, you can't call any function on every element of the collection, so why collect them together? Counting up how many you have is about all you can do. The ﬁxed-length restriction of the tuple allows us to pair up each contained value with its corresponding type, and then we know which functions are OK to call on which values.  \nOf course, it would be acceptable to form a list of values of diﬀerent types if you deﬁned a new type that is the logical disjunction of each diﬀerent one you need (say, int or float or int -> string), along with a tag so that every value of this new type is labeled to distinguish which case of the or it is. OCaml provides a mechanism to do exactly this, which we will see later on.  \n1 Tuples  \nFor now, back to tuples and lists. Here is a 4-tuple of someone's personal information:  \n\\# let person = (\"larry\", 47, 165, 'M');;  \nval person : string * int * int * char = (\"larry\", 47, 165, 'M')  \nIn general, the type of an n-tuple is written as an ordered sequence of n types, separated by * . The * is pronounced “cross” (think cross product), but more intuitively it means and. So, this personal information contains a string (name), an int (age), another int (weight), and a char (sex) . However, the tuple itself isnot written using *, because in expression-land that is the integer multiplication operator. To write down an n-tuple, we write the n values in order between parentheses and separated by commas.  \nAny tuple that has a diﬀerent number of values inside will have a diﬀerent type than any other tuple, and these types are not compatible. If I add a new element for height, I will have a 5-tuple; functions written for 4-tuples will not work on 5-tuples without modiﬁcation, and vice versa.  \nThe way to get values back out of a tuple is by pattern matching. This is an important concept that you'll see throughout the course. A pattern is like an expression, but it appears on the left side of an = or-> instead of on the right side. The pattern “looks like” the value of the expression it's matching, but if you put names where the sub-expressions would go, OCaml will bind the names to the corresponding values for you. Like so:  \n\\# let (name, age, _ , sex) = person;;  \nval name : string = \"larry\"  \nval age : int = 47  \nval sex : char = 'M'  \nBecause person is a 4-tuple, I need a pattern that looks like 4 things inside parens separated by commas. I'd like to extract the name, age, and sex, so I put those names into the appropriate spots in the pattern. I don't care about the person's weight for whatever reason, but because I'm writing a 4-tuple pattern, I must ﬁll in that spot; for this purpose, OCaml provides the dummy pattern name _ . Notice that all three values are extracted in one shot; conceptually, the pattern matches them all simultaneously.  \n1.1 The unit type  \nBesides n-tuples for n 􀀕 1 (a 1-tuple is just a single, i.e., a plain type with no *s like we had before), there is also a 0-tuple. To understand it, think of types as sets of values. The type int represents the set of all signed 31-bit integers; char represents the set of all unsigned 8-bit integers; the type float * int represents theset ","cbCaihAIbSU6ecIt","https://ap.wps.com/l/cbCaihAIbSU6ecIt","pdf",231433,"English","# Tuples and Lists\n## Tuples\n## The unit type\n## Lists","[{\"question\":\"What is the main difference between tuples and lists in OCaml?\",\"answer\":\"A tuple can store a fixed number of values of different types, while a list can store an unlimited number of values but all of the same type.\"},{\"question\":\"How do you extract values from an OCaml tuple?\",\"answer\":\"Use pattern matching with a pattern like (name, age, _, sex), where OCaml binds names to the corresponding tuple components and _ ignores an unwanted value.\"},{\"question\":\"Why does OCaml use the unit type and the value ()?\",\"answer\":\"The unit type represents exactly one element, () , conveying no information. It’s used when a function conceptually returns nothing, such as print_string and sequencing print operations.\"}]","OCaml Introduction: Tuples and Lists | PDF",13,[145,149,153,157],{"page_no":8,"url":146,"width":147,"height":148},"https://res-academy.cache.wpscdn.com/upload/kdocshare/2026-11-12/3967c6acd37f3f1773590ef087a0bd32.webp",983,1272,{"page_no":14,"url":150,"width":151,"height":152},"https://res-academy.cache.wpscdn.com/upload/kdocshare/2026-11-12/4cfbd107c191a73c8f5d955c0cfff5e1.webp",982,1270,{"page_no":81,"url":154,"width":155,"height":156},"https://res-academy.cache.wpscdn.com/upload/kdocshare/2026-11-12/272131af219e6ac67312e9831f0e8003.webp",981,1269,{"page_no":19,"url":158,"width":159,"height":160},"https://res-academy.cache.wpscdn.com/upload/kdocshare/2026-11-12/a9ba203804657423edfe1ef608ed7c0b.webp",980,1268,{"list":162,"token":66},[163,169,175,181,187,193,199,205,210,216],{"doc_id":164,"doc_title":165,"file_type":137,"home_preview_img_url":166,"slug":167,"view_count":19,"category_name":30,"create_tm":168},697980,"TUPLES AND DICTIONARIES - Introduction to Tuples","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230010146829/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796110439&Signature=VEvruw4ZkDkLsOzVnG2H1obMMuI%3D","tuples-and-dictionaries-introduction-to-tuples",1794446564,{"doc_id":170,"doc_title":171,"file_type":137,"home_preview_img_url":172,"slug":173,"view_count":24,"category_name":30,"create_tm":174},685778,"1-Tuples","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230000203953/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796110439&Signature=ig93FUkwCIqkMmm6sfI4wdYTE9c%3D","1-tuples",1794018230,{"doc_id":176,"doc_title":177,"file_type":137,"home_preview_img_url":178,"slug":179,"view_count":19,"category_name":30,"create_tm":180},685780,"Tuple in Python - Lecture 2","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230000203975/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796110439&Signature=A9IrYHN7%2FrU3Swti250N9aT5pTI%3D","tuple-in-python-lecture-2",1794018232,{"doc_id":182,"doc_title":183,"file_type":137,"home_preview_img_url":184,"slug":185,"view_count":81,"category_name":30,"create_tm":186},685772,"UNIT-IV - LIST, TUPLES, DICTIONARIES - Problem Solving and Python Programming","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230000203776/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796110439&Signature=tSnWVdkgXvKmfaewVWlyfCho3P4%3D","unit-iv-list-tuples-dictionaries-problem-solving-and-python-programming",1794018219,{"doc_id":188,"doc_title":189,"file_type":137,"home_preview_img_url":190,"slug":191,"view_count":34,"category_name":30,"create_tm":192},697976,"6.0001 Lecture 5 - Tuples, Lists, Aliasing, Mutability, Cloning","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230010146768/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796110439&Signature=h08dtsAG%2BxzSb7JNURqlnRXPPAY%3D","60001-lecture-5-tuples-lists-aliasing-mutability-cloning",1794446554,{"doc_id":194,"doc_title":195,"file_type":137,"home_preview_img_url":196,"slug":197,"view_count":81,"category_name":30,"create_tm":198},685777,"Lists and Tuples - Learn to create and use 1D lists, tuples and 2D lists","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230000203949/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796110439&Signature=V36alqKI3BZzehoLFStOOd4HLHk%3D","lists-and-tuples-learn-to-create-and-use-1d-lists-tuples-and-2d-lists",1794018226,{"doc_id":200,"doc_title":201,"file_type":137,"home_preview_img_url":202,"slug":203,"view_count":29,"category_name":30,"create_tm":204},697978,"Lecture 4: Tuples, List Slicing, List Comprehension, Strings","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230010146771/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796110439&Signature=ikeAhjcAOUud44uLs2s5fAUbh9g%3D","lecture-4-tuples-list-slicing-list-comprehension-strings",1794446559,{"doc_id":206,"doc_title":207,"file_type":137,"home_preview_img_url":208,"slug":209,"view_count":34,"category_name":30,"create_tm":186},685773,"Lists and Tuples - Chapter 7 - Programming Concepts","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230000203784/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796110439&Signature=iQeipD92qXsBslqR8ILSZlM%2Br0I%3D","lists-and-tuples-chapter-7-programming-concepts",{"doc_id":211,"doc_title":212,"file_type":137,"home_preview_img_url":213,"slug":214,"view_count":34,"category_name":30,"create_tm":215},685771,"Compound Data - Lists, Tuples, Dictionaries - Unit IV - Learning Notes","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230000203726/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796110439&Signature=JtlTBBVTF6bRYwV1CTtzA%2FcsYFg%3D","compound-data-lists-tuples-dictionaries-unit-iv-learning-notes",1794018217,{"doc_id":217,"doc_title":218,"file_type":137,"home_preview_img_url":219,"slug":220,"view_count":24,"category_name":30,"create_tm":221},685782,"1.4.2 - Data Structures - Advanced Notes - Specification","https://weboffice-temporary-prod-sg.obs.ap-southeast-3.myhuaweicloud.com:443/export/thumbnail/export/thumbnail/110230000208907/last/300/407?AWSAccessKeyId=HPUAE6OPDRI9QZXR6WJG&Expires=1796110439&Signature=a9MSSe3nsoyS9JxAQvzYLfx1lIw%3D","142-data-structures-advanced-notes-specification",1794018468]