Skip to content

Commit fdcaadb

Browse files
committed
added back short examples in code_snippets_for_book
1 parent b3906fd commit fdcaadb

File tree

7 files changed

+64
-1
lines changed

7 files changed

+64
-1
lines changed

manuscript/input_output.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ In this last example, we passed the file name as a string to the macro **with-op
5656
(make-pathname :directory "testdata"
5757
:name "test.dat")))
5858
(with-open-file
59-
(input-stream a-path-name :direction :input)
59+
(input-stream a-path-name :direction :input)))
6060
~~~~~~~~
6161

6262
Here, we are specifying that we want to use the file **test.dat** in the subdirectory **testdata**. Note: I almost never use pathnames. Instead, I specify files using a string and the character / as a directory delimiter. I find this to be portable for the Macintosh, Windows, and Linux operating systems using all Common Lisp implementations.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
(flet ((add-one (x)
2+
(+ x 1))
3+
(add-two (x)
4+
(+ x 2)))
5+
(format t "redefined variables: ~A ~A~%" (add-one 100) (add-two 100)))
6+
7+
(let ((a 3.14))
8+
(defun test2 (x) ; this works, but don't do it!
9+
(print x))
10+
(test2 a))
11+
12+
(test2 50)
13+
14+
(let ((x 1)
15+
(y 2))
16+
;; properly define a test function nested inside a let statement:
17+
(flet ((test (a b)
18+
(let ((z (+ a b)))
19+
;; define a helper function nested inside a let/function/let:
20+
(flet ((nested-function (a)
21+
(+ a a)))
22+
(nested-function z)))))
23+
;; call nested function 'test':
24+
(format t "test result is ~A~%" (test x y))))
25+
26+
(let ((z 10))
27+
(labels ((test-recursion (a)
28+
(format t "test-recursion ~A~%" (+ a z))
29+
(if (> a 0)
30+
(test-recursion (- a 1)))))
31+
(test-recursion 5)))
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
(defun read-from-string-test ()
2+
"read a maximum of 1000 expressions from a string"
3+
(let ((str "1 2 \"My parrot is named Brady.\" (11 22)"))
4+
(with-input-from-string
5+
(input-stream str)
6+
(dotimes (i 1000)
7+
(let ((x (read input-stream nil nil)))
8+
(if (null x) (return)) ;; break out of the 'dotimes' loop
9+
(format t "next expression in string: ~S~%" x))))))
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
(defun read-test-1 ()
2+
"read a maximum of 1000 expressions from the file 'test.dat'"
3+
(with-open-file
4+
(input-stream "test.dat" :direction :input)
5+
(dotimes (i 1000)
6+
(let ((x (read input-stream nil nil)))
7+
(if (null x) (return)) ;; break out of the 'dotimes' loop
8+
(format t "next expression in file: ~S~%" x)))))
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
(let ((a-path-name
2+
(make-pathname :directory "testdata"
3+
:name "test.dat")))
4+
(with-open-file
5+
(input-stream a-path-name :direction :input)))
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
(defun readline-test ()
2+
"read a maximum of 1000 expressions from the file 'test.dat'"
3+
(with-open-file
4+
(input-stream "test.dat" :direction :input)
5+
(dotimes (i 1000)
6+
(let ((x (read-line input-stream nil nil)))
7+
(if (null x) (return)) ;; break out of the 'dotimes' loop
8+
(format t "next line in file: ~S~%" x)))))
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
1 2 3
2+
4 "the cat bit the rat"

0 commit comments

Comments
 (0)