LISP - SEQUENCES. The function make-sequence allows you to create a sequence of any type. The syntax for this function is:

Similar documents
INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. More Common Lisp

LISP - QUICK GUIDE LISP - OVERVIEW

Symbolic Programming. Dr. Zoran Duric () Symbolic Programming 1/ 89 August 28, / 89

Functional programming with Common Lisp

Common LISP Tutorial 1 (Basic)

CS 480. Lisp J. Kosecka George Mason University. Lisp Slides

Common LISP-Introduction

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

Type-checking on Heterogeneous Sequences

Common Lisp. Blake McBride

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Allegro CL Certification Program

Introduction to Programming Using Java (98-388)

Functional Programming. Pure Functional Programming

Functions, Conditionals & Predicates

Pattern Matching and Abstract Data Types

CSCI337 Organisation of Programming Languages LISP

Introduction to Lisp

LISP. Everything in a computer is a string of binary digits, ones and zeros, which everyone calls bits.

SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION

Lecture #5 Kenneth W. Flynn RPI CS

Look at the outermost list first, evaluate each of its arguments, and use the results as arguments to the outermost operator.

Imperative, OO and Functional Languages A C program is

COMPUTER PROGRAMMING LOOPS

Lecture Notes on Lisp A Brief Introduction

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. Common Lisp Fundamentals

Allegro CL Certification Program

Recursion & Iteration

SWIFT - CLOSURES. Global Functions Nested Functions Closure Expressions. Have a name. Capture values from enclosing function

Symbolic Computation and Common Lisp

SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION

INF121: Functional Algorithmic and Programming

SECTION II: LANGUAGE BASICS

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

Documentation for LISP in BASIC

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

CS 6353 Compiler Construction Project Assignments

POETRY OF PROGRAMMING CODE READING EXERCISES IN CLOJURE V

JSON. Version 6.6. Eli Barzilay and Dave Herman. July 22, 2016

Lisp. Versions of LISP

Control Structures. A program can proceed: Sequentially Selectively (branch) - making a choice Repetitively (iteratively) - looping

Haskell 98 in short! CPSC 449 Principles of Programming Languages

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

C# MOCK TEST C# MOCK TEST I

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

C++ (Non for C Programmer) (BT307) 40 Hours

A Genetic Algorithm Implementation

CSC Web Programming. Introduction to SQL

F# - ARRAYS. Arrays are fixed-size, zero-based, mutable collections of consecutive data elements that are all of the same type.

CSC Web Programming. Introduction to JavaScript

RUBY OPERATORS. Ruby Arithmetic Operators: Ruby Comparison Operators:

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

The Lorax Programming Language

Selection Statement ( if )

Putting the fun in functional programming

Computers Programming Course 6. Iulian Năstac

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

Programming for the Web with PHP

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

Basics of Java: Expressions & Statements. Nathaniel Osgood CMPT 858 February 15, 2011

Functional Programming with Common Lisp

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4.

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

Introduction to LISP. York University Department of Computer Science and Engineering. York University- CSE V.

Pace University. Fundamental Concepts of CS121 1

C# MOCK TEST C# MOCK TEST II

Chapter 1. Fundamentals of Higher Order Programming

Data Structures and Algorithms. Chapter 1

Lecture #2 Kenneth W. Flynn RPI CS

Expressions and Assignment

1 P a g e A r y a n C o l l e g e \ B S c _ I T \ C \

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Python allows variables to hold string values, just like any other type (Boolean, int, float). So, the following assignment statements are valid:

ACT-R RPC Interface Documentation. Working Draft Dan Bothell

Reasoning About Programs Panagiotis Manolios

Long Questions. 7. How does union help in storing the values? How it differs from structure?

Index COPYRIGHTED MATERIAL

PLT Fall Shoo. Language Reference Manual

4. Inputting data or messages to a function is called passing data to the function.

CSI33 Data Structures

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 2. ÚVOD DO LISPU: ATOMY, SEZNAMY, FUNKCE,

Introduction to ACL2. CS 680 Formal Methods for Computer Verification. Jeremy Johnson Drexel University

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 3. LISP: ZÁKLADNÍ FUNKCE, POUŽÍVÁNÍ REKURZE,

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

Introductory Scheme. Revision 1

PYTHON MOCK TEST PYTHON MOCK TEST III

Introduction. chapter Functions

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

CSc 372. Comparative Programming Languages. 2 : Functional Programming. Department of Computer Science University of Arizona

C Programming Language (Chapter 2 of K&R) Variables and Constants

Semantic Processing (Part 2)

CS313D: ADVANCED PROGRAMMING LANGUAGE

Chapter 2: Using Data

Petros: A Multi-purpose Text File Manipulation Language

CMSC 331 Final Exam Section 0201 December 18, 2000

Programming Paradigms

Transcription:

http://www.tutorialspoint.com/lisp/lisp_sequences.htm LISP - SEQUENCES Copyright tutorialspoint.com Sequence is an abstract data type in LISP. Vectors and lists are the two concrete subtypes of this data type. All the functionalities defined on sequence data type are actually applied on all vectors and list types. In this section, we will discuss most commonly used functions on sequences. Before starting on various ways of manipulating sequences i. e., vectorsandlists, let us have a look at the list of all available functions. Creating a Sequence he function make-sequence allows you to create a sequence of any type. he syntax for this function is: make-sequence sqtype sqsize &key :initial-element It creates a sequence of type sqtype and of length sqsize. You may optionally specify some value using the :initial-element argument, then each of the elements will be initialized to this value. For example, (write (make-sequence '(vector float) 10 :initial-element 1.0)) #(1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0 1.0) Generic Functions on Sequences Function elt length subseq copy-seq fill replace count reverse nreverse concatenate Description It allows access to individual elements through an integer index. It returns the length of a sequence. It returns a sub-sequence by extracting the subsequence starting at a particular index and continuing to a particular ending index or the end of the sequence. It returns a sequence that contains the same elements as its argument. It is used to set multiple elements of a sequence to a single value. It takes two sequences and the first argument sequence is destructively modified by copying successive elements into it from the second argument sequence. It takes an item and a sequence and returns the number of times the item appears in the sequence. It returns a sequence contains the same elements of the argument but in reverse order. It returns the same sequence containing the same elements as sequence but in reverse order. It creates a new sequence containing the concatenation of any number of

sequences. position find sort merge map some every notany notevery reduce search remove delete substitute nsubstitute mismatch It takes an item and a sequence and returns the index of the item in the sequence or nil. It takes an item and a sequence. It finds the item in the sequence and returns it, if not found then it returns nil. It takes a sequence and a two-argument predicate and returns a sorted version of the sequence. It takes two sequences and a predicate and returns a sequence produced by merging the two sequences, according to the predicate. It takes an n-argument function and n sequences and returns a new sequence containing the result of applying the function to subsequent elements of the sequences. It takes a predicate as an argument and iterates over the argument sequence, and returns the first non- value returned by the predicate or returns false if the predicate is never satisfied. It takes a predicate as an argument and iterate over the argument sequence, it terminates, returning false, as soon as the predicate fails. If the predicate is always satisfied, it returns true. It takes a predicate as an argument and iterate over the argument sequence, and returns false as soon as the predicate is satisfied or true if it never is. It takes a predicate as an argument and iterate over the argument sequence, and returns true as soon as the predicate fails or false if the predicate is always satisfied. It maps over a single sequence, applying a two-argument function first to the first two elements of the sequence and then to the value returned by the function and subsequent elements of the sequence. It searches a sequence to locate one or more elements satisfying some test. It takes an item and a sequence and returns the sequence with instances of item removed. his also takes an item and a sequence and returns a sequence of the same kind as the argument sequence that has the same elements except the item. It takes a new item, an existing item, and a sequence and returns a sequence with instances of the existing item replaced with the new item. It takes a new item, an existing item, and a sequence and returns the same sequence with instances of the existing item replaced with the new item. It takes two sequences and returns the index of the first pair of mismatched elements. Standard Sequence Function Keyword Arguments Argument Meaning Default Value :test :key It is a two-argument function used to compare item orvalueextractedby: keyfunction to element. One-argument function to extract key value from actual sequence element. means use element as is. EQL :start Starting index inclusive of subsequence. 0

:end :from-end :count Ending index exclusive of subsequence. indicates end of sequence. If true, the sequence will be traversed in reverse order, from end to start. Number indicating the number of elements to remove or substitute or to indicate all REMOVEandSUBSIUEonly. We have just discussed various functions and keywords that are used as arguments in these functions working on sequences. In the next sections, we will see how to use these functions using examples. Finding Length and Element he length function returns the length of a sequence, and the elt function allows you to access individual elements using an integer index. (setq x (vector 'a 'b 'c 'd 'e)) (write (length x)) (write (elt x 3)) 5 D Modifying Sequences Some sequence functions allows iterating through the sequence and perform some operations like, searching, removing, counting or filtering specific elements without writing explicit loops. he following example demonstrates this: 1 (write (count 7 '(1 5 6 7 8 9 2 7 3 4 5))) (write (remove 5 '(1 5 6 7 8 9 2 7 3 4 5))) (write (delete 5 '(1 5 6 7 8 9 2 7 3 4 5))) (write (substitute 10 7 '(1 5 6 7 8 9 2 7 3 4 5))) (write (find 7 '(1 5 6 7 8 9 2 7 3 4 5))) (write (position 5 '(1 5 6 7 8 9 2 7 3 4 5))) 2 (1 6 7 8 9 2 7 3 4) (1 6 7 8 9 2 7 3 4) (1 5 6 10 8 9 2 10 3 4 5) 7 1

2 (write (delete-if #'oddp '(1 5 6 7 8 9 2 7 3 4 5))) (write (delete-if #'evenp '(1 5 6 7 8 9 2 7 3 4 5))) (write (remove-if #'evenp '(1 5 6 7 8 9 2 7 3 4 5) :count 1 :from-end t)) (setq x (vector 'a 'b 'c 'd 'e 'f 'g)) (fill x 'p :start 1 :end 4) (write x) (6 8 2 4) (1 5 7 9 7 3 5) (1 5 6 7 8 9 2 7 3 5) #(A P P P E F G) Sorting and Merging Sequences he sorting functions take a sequence and a two-argument predicate and return a sorted version of the sequence. 1 (write (sort '(2 4 7 3 9 1 5 4 6 3 8) #'<)) (write (sort '(2 4 7 3 9 1 5 4 6 3 8) #'>)) (1 2 3 3 4 4 5 6 7 8 9) (9 8 7 6 5 4 4 3 3 2 1) 2 (write (merge 'vector #(1 3 5) #(2 4 6) #'<)) (write (merge 'list #(1 3 5) #(2 4 6) #'<)) #(1 2 3 4 5 6) (1 2 3 4 5 6) Sequence Predicates he functions every, some, notany, and notevery are called the sequence predicates. hese functions iterate over sequences and test the Boolean predicate. All these functions takes a predicate as the first argument and the remaining arguments are

sequences. (write (every #'evenp #(2 4 6 8 10))) (write (some #'evenp #(2 4 6 8 10 13 14))) (write (every #'evenp #(2 4 6 8 10 13 14))) (write (notany #'evenp #(2 4 6 8 10))) (write (notevery #'evenp #(2 4 6 8 10 13 14))) Mapping Sequences We have already discussed the mapping functions. Similarly the map function allows you to apply a function on to subsequent elements of one or more sequences. he map function takes a n-argument function and n sequences and returns a new sequence after applying the function to subsequent elements of the sequences. (write (map 'vector #'* #(2 3 4 5) #(3 5 4 8))) #(6 15 16 40) Loading [MathJax]/jax/output/HML-CSS/jax.js