Common Lisp Study notes (vii)

Source: Internet
Author: User
Tags setf

    • 7 Applicative Programming
      • 7.2 Funcall
      • 7.3 Mapcar
      • 7.4 Manipulating tables with Mapcar
      • 7.5 Lambda expressions
      • 7.6 find-if
      • 7.7 My-assoc
      • 7.8 remove-if, Remove-if-not
      • 7.9 Reduce
      • 7.10 every
      • Debug Tool:trace
      • 7.11 Operating on multiple lists
      • 7.12 function functions
      • 7.13 Kwargs for applicative operators
7 applicative Programming7.2 Funcall

(Funcall # ' cons ' a ' B '), (A. b)
funcallFunctions can be called by function name

Lisp inside quote function using symbol # '

Let's look at an example,

> (SETF fn # ' cons) #<compilied-function cons {6041410}>> (funcall fn ' C ' d) (c. D)
7.3 Mapcar

mapcarYou can apply a function to each element in a list, and then put the results of each return back in a list

(Defun Square (n) (* n N)) (square 3), 9 (Square ' (1 2 3)), Error (Mapcar # ' Square ' (1 2 3), (1 4 9)
7.4 Manipulating tables with Mapcar

(Mapcar # ' Function-name list)


Now there is a conversion table for English and French

(setf words  ' (one un) (    deux) (three trois) (four quatre) (    five cinq))


Gets the first item of all lists:

> (Mapcar # ' first words) (one, three four five)

EX 7.1

(Defun add1 (x) (+ x 1)) > (Mapcar # ' Add1 ' (1 3 5 7 9)
7.5 Lambda expressions

(Lambda (n) (* n N))


If you mapcar do not want to define a function in use, you can define the anonymous function lambda directly inside the Mapcar function.

> (Mapcar # ' (Lambda (n) (* n N)) ' (1 2 3) (1 4 9)


Ex 7.7

(Defun flips (x)   (Mapcar # ' (Lambda (n) (if (equal n ' Up) ' down ') x))
7.6 find-if

(find-if # ' predicate list)


Call predicate for each item in the list, return the item with the first return value of T, and return nil without returning t

> (find-if # ' ODDP ' (2 4 6 7 8)) 7
7.7 My-assoc

The ASSOC function has 2 parameters (key table), which returns the list of the first element in the table that is key.


Now you can write a function of assoc function with Find-if.

(Defun MY-ASSOC (key table)  (Find-if # ' (Lambda (entry) (Equal key (first entry)) table))

For each list in the table, we define the lambda function to determine if the key is equal to the first element of the list, and if you want to return T, the Find-if function returns the list as long as the lambda function returns T. Just can implement the function of Assoc function

One point to note is that the lambda function in the example can access the local variable key defined in the MY-ASSOC function, stating that the lambda function can contain the local vars of the outer function of the lambda function


Ex 7.8

(Defun foo (x k)   (Find-if # ' (Lambda (n) (if (< n (-K)) Nil (<= N (+ k)))))


Ex 7.9

(Defun find-nested (x)  (Find-if # ' (Lambda (Entry) (and (LISTP entry) (not (equal entry nil))))
7.8 remove-if, Remove-if-not

(remove-if # ' Function-name list)


For each element in the list, the function is called, and if T is deleted, the remaining list is returned.

> (remove-if # ' Numberp ' (2 for 1 sale) (For sale)


Remove-if-not, conversely, if the function returns nil, the element is deleted

> (Remove-if-not # ' (Lambda (x) (> x 3)) ' (2 3 4 5 6)) (4 5 6)


Ex 7.15

(Defun rank (x) (first x)) (Defun Suit (x) (second x)) (Setf My-hand ' ((3 Hearts) (5 clubs) (2 diamonds) (4 diamonds) (Ace Spades))) (Defun count-suit (suit hand) (Length (Remove-if-not # ' (Lambda (entry) (Equal suit (second entry))))) (SETF colors ' (clubs black) (Diamonds red) (Hearts red) (Spades black)) (Defun color-of (x) (Second (Assoc (second x) colors))) (Defun first-red (hand) (Find-if # ' (Lambda (card) (Equal ' red (color-of Card)))) (Defun black-cards (hand) (Remove-if-not # ' (Lambda (card) (Equal ' black (color-of Card)))) (Defun what-ranks (suit hand) (Mapcar # ' First (Remove-if-not # ' (Lambda (card) (Equal suit (second card))))) (SETF all-ranks ' (2 3 4 5 6 7 8 9 ten Jack Queen King Ace)) (Defun higher-rank-p (Card1 Card2) (Member (first card1) (Rest (Member (first card2) all-ranks))) (Defun high-card (hand); Build a rank list (Ace King Queen Jack 10 ... 2), use Find-if to find out whether the rank of this list appears in hand;;( Mapcar # ' first hand ' can get the list of all rank in hand (Assoc (find-if # ' (LamBDA (rank) (Member rank (Mapcar # ' first hand))) (reverse all-ranks)) (hand)) 
7.9 Reduce

(Reduce # ' function list)


reduceThe function continues to operate on the list until only one result is left, such as adding to a list and summing all the factors in the list

For example, the list is (a B c d), then the result of a B is computed, and then the result and the C operation, the result is again with the D operation, and finally left a result


Where function must be two parameters, eg,

(Reduce # ' + ' (1 2 3)), 6 (Reduce # ' * ' (2 3 4)) 24


Reduce can change the nested list to a list,eg,

> (Reduce # ' append ' ((One un) (deux) (three trois)) (One un-one, deux three trois)


Ex 7.17

(Defun total-length (x)  (Reduce # ' + (Mapcar # ' length x)))
7.10 every

(Every # ' predicate list)
If all the elements in the list make predicate return T, the function returns T, meaning that the factors in each list are true, otherwise the NIL,EG is returned,

> (Every # ' Numberp ' (1 2 3)) T


If the second parameter is nil, then every returns T

Every can also be given to multiple list,eg after the

> (Every # ' > ' (1 4)) T

>The function requires two parameters, every each time from the back of the two list to take one as a parameter, as the example of 10>1,20>4,30>11,40>24 is true, so return T

Debug Tool:trace
    • (Trace func1 Func2 ...)
    • (trace): View the function that is being trace
    • (Untrace func1 Func2 ...)
    • (untrace): Untrace all functions
(defun half (n) (* n 0.5)) (Defun average (x y)  (+ (half x) (half y))) > (trace Half average) (half average) > (average 3 7) 1. Trace: (AVERAGE ' 3 ' 7) 2. Trace: (Half ' 3) 2. Trace:half ==> 1.52. Trace: (Half ' 7) 2. Trace:half ==> 3.51. Trace:average ==> 5.05.0

Ex 7.29

(SETF database ' (B1 shape brick) (B1 color green) (B1 size Small) (B1 supported-by B2) (B1 supported-by B3 ) (B2 shape brick) (B2 color red) (B2 size Small) (B2 supports B1) (B2 left-of B3) (B3 shape Brick) ( B3 color red) (B3 size Small) (B3 supports B1) (B3 right-of B2) (B4 shape pyramid) (B4 color blue) (B4 s ize Large) (B4 supported-by b5) (B5 shape cube) (B5 color green) (B5 size Large) (B5 supports B4) (B6 sh Ape brick) (B6 color Purple) (B6 size Large))) (Defun match-element (x y) (or (Equal x y) (Equal y '))) (Defun match-triple (assertion pattern) (every # ' match-element assertion pattern) (Defun Fetch (pattern) (Remove-if-not # ' (Lambda (entry) (Match-triple entry pattern)) > (Fetch ' (B4 shape?) > (Fetch ' (? Shape brick)) > (Fetch ' (B2 b3)) > (Fetch ' (? Color?)) > (Fetch ' (b4??)) (Defun ask-color (block) (List block ' Color '?)) (Defun supporters (block) (Mapcar # ' first (Fetch (list ')? (supports block))) (Defun supp-cube (Block) (Find-if # ' (Lambda (Entry) (Fetch (List entry ' shape ' cube)) (Supporters Blo CK)) (Defun DESC1 (Block) (Remove-if-not # ' (Lambda (entry) (Equal block (first entry))) (Defun DESC2 (Block) (  Mapcar # ' Rest (Desc1 block)) (Defun description (block) (Reduce # ' append (DESC2 block)) (SETF (CDR (last Database)) ' ((B1 Material wood) (B2 material plastic)))
7.11 Operating on multiple lists

Before we used to mapcar call a function with each item of a list as a parameter, this function only accepts one parameter. In fact, Mapcar can also be used for multi-parameter functions, eg,

> (Mapcar # ' (Lambda (x y) (list x ' Get Y) ' (Fred Wilma) ' (Job1 job2) (Fred gets Job1) (Wilma gets Job2) > (mapcar # ' + ' (1 2 3) ' (10 20 30 40) (11 22 33)

This approach is similar to the previous every list, if the length of the two list is not the same, then the shorter one will stop


Ex 7.30

(Mapcar # ' Append Words (mapcar # ' list ' (Uno dos tres quatro cinco))
7.12 function functions

' Represent function quote , # ' means function function , # ' cons equivalence with (function cons)

> ' conscons> # ' cons#<compiled-function cons ...>> # ' (Lambda (x) (...)) #<lexical-closure ...>

# ' Returns a function object that can be called with Funcall

> (setf G # ' (Lambda (x) (* x))) #<lexical...>> (Funcall G 12) 120
7.13 Kwargs for applicative operators

:from-endsuch as keyword parameters can also be applied to find-if,remove-if,reduce these functions

> (Reduce # ' Cons ' (a b C D e)) ((((b). c). D). e) > (Reduce # ' Cons ' (a b c d e): From-end t) (a B c d. e)

Common Lisp Study notes (vii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.