DOC PREVIEW
UMBC CMSC 331 - Side Effects

This preview shows page 1 out of 3 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

111SideSideEffectsEffects22UMBCUMBCan Honors University in Marylandan Honors University in MarylandSide effects in functional languagesSide effects in functional languagesPure function languages donPure function languages don’’t have sidet have side--effects except for things like printingeffects except for things like printinge.g., no assignment operator!e.g., no assignment operator!Most successful languages arenMost successful languages aren’’t pure, t pure, thotho..Lisp has many built in functions that have Lisp has many built in functions that have side effectsside effectsGood lisp programs use them in Good lisp programs use them in moderationmoderation33UMBCUMBCan Honors University in Marylandan Honors University in MarylandIn the beginning there was setIn the beginning there was set;; set is Lisp;; set is Lisp’’s assignment statements assignment statement;;;;> (SET (quote A) 100)> (SET (quote A) 100)100100> A> A10010044UMBCUMBCan Honors University in Marylandan Honors University in Marylandset begat set begat setqsetq;; people got tired of writing all the quote;; people got tired of writing all the quote’’ss> (> (setqsetqa 100)a 100)100100;; but of course this has no more chars;; but of course this has no more chars……> (set > (set ‘‘b 200)b 200)200200> (> (setqsetqa 100 b 200 c 300)a 100 b 200 c 300)300300255UMBCUMBCan Honors University in Marylandan Honors University in MarylandAnd And setqsetqbegat setfbegat setfSetf is common lisps Setf is common lisps ““generalizedgeneralized””assignment statement and can be used to assignment statement and can be used to assign values to variablesassign values to variables……and also to modify many kinds of data and also to modify many kinds of data structures (e.g., arrays, structures, objects, structures (e.g., arrays, structures, objects, ……))……more on this shortlymore on this shortlyFirst letFirst let’’s look at some of the other data s look at some of the other data structuresstructures66UMBCUMBCan Honors University in Marylandan Honors University in MarylandRplacaRplacaand and rplacdrplacd>(>(setqsetqx x ‘‘(a b c))(a b c))(a b c)(a b c)> (> (rplacarplacax x ‘‘foo) ;; foo) ;; rplacarplaca= replace car= replace car(foo b c)(foo b c)> x> x(foo b c)(foo b c)> (> (rplacdrplacdx x ‘‘(2 3)) ;; (2 3)) ;; rplacarplaca= replace car= replace car(foo 2 3)(foo 2 3)> x> x(foo 2 3)(foo 2 3)Caution: Don’t try this at home. For hackers only!77UMBCUMBCan Honors University in Marylandan Honors University in MarylandRplacaRplacaand and rplacdrplacd>(>(setqsetqx x ‘‘(a b c))(a b c))(a b c)(a b c)> (> (rplacarplacax x ‘‘foo)foo)(foo b c)(foo b c)> x> x(foo b c)(foo b c)> (> (rplacdrplacdx x ‘‘(2 3)) (2 3)) (foo 2 3)(foo 2 3)> x> x(foo 2 3)(foo 2 3)abcxabcxfooabcxfoo2388UMBCUMBCan Honors University in Marylandan Honors University in MarylandSetf as a generalized assignmentSetf as a generalized assignment(setf <place1> <val1> (setf <place1> <val1> <place2> <val2> . . <place2> <val2> . . <<placeNplaceN> <> <valNvalN>)>)Where <place> is eitherWhere <place> is eithera variable namea variable namean expression referring to part of a larger structure an expression referring to part of a larger structure (e.g. a list, property list, structure, or array). (e.g. a list, property list, structure, or array). <<valval>: any LISP expression. >: any LISP expression.399UMBCUMBCan Honors University in Marylandan Honors University in Marylandexampleexample[2]> (setf list '(1 2 (3.1 3.2) 4 5))[2]> (setf list '(1 2 (3.1 3.2) 4 5))(1 2 (3.1 3.2) 4 5)(1 2 (3.1 3.2) 4 5)[3]> (setf (car list) 100) [3]> (setf (car list) 100) ;; like (rplaca list 100)100100[4]> list[4]> list(100 2 (3.1 3.2) 4 5)(100 2 (3.1 3.2) 4 5)[5]> (setf (fourth list) 400) [5]> (setf (fourth list) 400) ;; like (rplaca (cdddr list) 400)400400[6]> list[6]> list(100 2 (3.1 3.2) 400 5)(100 2 (3.1 3.2) 400 5)[7]> (setf (cdr (third list)) nil) [7]> (setf (cdr (third list)) nil) ;; like (rplacd (caddr list) nil)NILNIL[8]> list[8]> list(100 2 (3.1) 400 5)(100 2 (3.1) 400 5)1010UMBCUMBCan Honors University in Marylandan Honors University in MarylandArraysArrays[1]> (setf a (make[1]> (setf a (make--array '(3 4)))array '(3 4)))#2A((NIL NIL #2A((NIL NIL NILNILNILNIL) (NIL NIL ) (NIL NIL NILNILNILNIL) (NIL NIL ) (NIL NIL NILNILNILNIL))))[2]> ([2]> (arefarefa 2 3)a 2 3)NILNIL[3]> (loop for i from 0 to 2[3]> (loop for i from 0 to 2do (loop for j from 0 to 3do (loop for j from 0 to 3do (setf (do (setf (arefarefa i j) (+ i j))))a i j) (+ i j))))NILNIL[4]> ([4]> (arefarefa 2 3)a 2 3)55[5]> a[5]> a#2A((0 1 2 3) (1 2 3 4) (2 3 4 5))#2A((0 1 2 3) (1 2 3 4) (2 3 4 5))1111UMBCUMBCan Honors University in Marylandan Honors University in MarylandStructuresStructures>(setf crew '(>(setf crew '(picardpicardrikerrikerworfworfcrusher))crusher))(PICARD RIKER WORF CRUSHER)(PICARD RIKER WORF CRUSHER)>(setf (first crew) (list 'captain (first crew))>(setf (first crew) (list 'captain (first crew))(second crew) (list 'commander (second crew))(second crew) (list 'commander (second crew))(third crew) (list 'lieutenant (third crew)) (third crew) (list 'lieutenant (third crew)) (fourth crew) (list 'ensign (fourth crew)))(fourth crew) (list 'ensign (fourth crew)))(ENSIGN CRUSHER)(ENSIGN CRUSHER)>crew>crew((CAPTAIN PICARD) (COMMANDER RIKER) (LIEUTENANT WORF) (ENSIGN CR((CAPTAIN PICARD) (COMMANDER RIKER) (LIEUTENANT WORF) (ENSIGN CRUSHER))USHER))> (> (defstructdefstructstarship crew captain)starship crew captain)STARSHIPSTARSHIP>(setf enterprise (make>(setf enterprise (make--starship))starship))#S(STARSHIP CREW NIL CAPTAIN NIL)#S(STARSHIP CREW NIL CAPTAIN NIL)>(setf (starship>(setf (starship--crew enterprise) (rest crew)crew enterprise) (rest crew)(starship(starship--captain enterprise) (second (first crew)))captain enterprise) (second (first crew)))PICARDPICARD>enterprise>enterprise#S(STARSHIP CREW#S(STARSHIP CREW((COMMANDER RIKER) (LIEUTENANT WORF) (ENSIGN CRUSHER))((COMMANDER RIKER) (LIEUTENANT WORF) (ENSIGN CRUSHER))CAPTAIN PICARD)CAPTAIN PICARD)1212UMBCUMBCan Honors University in Marylandan Honors University in


View Full Document

UMBC CMSC 331 - Side Effects

Documents in this Course
Semantics

Semantics

14 pages

Java

Java

12 pages

Java

Java

31 pages

V

V

46 pages

Semantics

Semantics

11 pages

Load more
Download Side Effects
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Side Effects and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Side Effects 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?