Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

lisp cons two lists

  1. User

    lisp cons two lists

  2. Assistant

    In Lisp, the cons function can be used to create a new list by adding an element to the beginning of an existing list. To concatenate, or join, two lists together, you can use the append function. Here are examples of both methods:

    Using cons:

    (setq list1 '(1 2 3))
    (setq list2 '(4 5 6))
    (cons list1 list2)   ; creates a new list containing both original lists
    

    Output: ( (1 2 3) 4 5 6 )

    Using append:

    (setq list1 '(1 2 3))
    (setq list2 '(4 5 6))
    (append list1 list2)   ; concatenates list2 onto list1
    

    Output: (1 2 3 4 5 6)