0% found this document useful (0 votes)
6 views13 pages

List Types

The document discusses list types in functional programming languages, specifically LISP, Scheme, and ML. It explains fundamental list operations such as CAR, CDR, CONS, and LIST, along with their usage and syntax. Additionally, it highlights differences in list representation and operations between LISP and ML.

Uploaded by

M Nandini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views13 pages

List Types

The document discusses list types in functional programming languages, specifically LISP, Scheme, and ML. It explains fundamental list operations such as CAR, CDR, CONS, and LIST, along with their usage and syntax. Additionally, it highlights differences in list representation and operations between LISP and ML.

Uploaded by

M Nandini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

List Types

• first supported in the first functional


programming language, LISP.
• Lists in Scheme and Common LISP are delimited
by parentheses and the elements are not
separated by any punctuation. For example,
• (A B C D)
• Nested lists have the same form
• (A (B C) D)
• Data and code have the same syntactic form in
LISP and its descendants.
• If the list (A B C) is interpreted as code, it is a
call to the function A with parameters B and C.
• The fundamental list operations in Scheme are
two functions that take lists apart and two
that build lists.
• CAR function
• CDR function
• The CAR function returns the first element of
its list parameter.
• (CAR '(A B C))
• The quote before the parameter list is to
prevent the interpreter from considering the
list a call to the A function with the
parameters B and C, in which case it would
interpret it. This call to CAR returns A
• The CDR function returns its parameter list
minus its first element.
• For example, consider the following example:
• (CDR '(A B C))
• This function call returns the list (B C).
• Common LISP also has the functions FIRST
(same as CAR), SECOND, . . . ,TENTH, which
return the element of their list parameters
that is specified by their names.
• In Scheme and Common LISP, new lists are
constructed with the CONS and LIST functions.
• The function CONS takes two parameters and
returns a new list with its first parameter as the
first element and its second parameter as the
remainder of that list. For example, consider
the following:
• (CONS 'A '(B C))
• This call returns the new list (A B C).
• The LIST function takes any number of
parameters and returns a new list with the
parameters as its elements.
• For example, consider the following call to
LIST:
• (LIST 'A 'B '(C D))
• This call returns the new list (A B (C D)).
• ML has lists and list operations
• Lists are specified in square brackets, with the
elements separated by commas
• [5, 7, 9]
• The Scheme CONS function is implemented as
a binary infix operator in ML, represented as ::.
• For example,
• 3 :: [5, 7, 9]
• returns the following new list: [3, 5, 7, 9].
• The elements of a list must be of the same
type, so the following list would be illegal:
• [5, 7.3, 9]
• ML has functions that correspond to Scheme’s
CAR and CDR, named hd (head) and tl (tail).
For example,
• hd [5, 7, 9] is 5
• tl [5, 7, 9] is [7, 9]

You might also like