Skip to content

Latest commit

 

History

History
19 lines (14 loc) · 1.48 KB

questions.md

File metadata and controls

19 lines (14 loc) · 1.48 KB

Write a function sum_list that takes a list of integers and returns the sum of all the elements in the list using pattern matching. Write a function head_option that takes a list and returns Some of the head element if the list is non-empty, and None if the list is empty. Pattern Matching with Tuples:

Write a function swap that takes a tuple (a, b) and returns a tuple (b, a) using pattern matching. Write a function fst3 that takes a triple (a, b, c) and returns the first element. Advanced Pattern Matching:

Write a function zip that takes two lists and returns a list of pairs, where each pair contains corresponding elements from the two lists. If one list is longer than the other, ignore the extra elements. Write a function count_occurrences that takes a list of integers and an integer x, and returns the number of times x appears in the list. Pattern Matching with Option Types:

Write a function option_map that takes a function f and an option value. If the value is Some x, apply f to x and return Some of the result. If the value is None, return None. Pattern Matching with Records:

Define a record type person with fields name (string) and age (int). Write a function is_adult that takes a person record and returns true if the person is 18 or older, and false otherwise using pattern matching. Nested Pattern Matching:

Write a function flatten that takes a list of lists and returns a single list containing all the elements of the sublists, in the same order, using nested pattern matching.