SyntaxHighlighter

Показаны сообщения с ярлыком Sicp. Показать все сообщения
Показаны сообщения с ярлыком Sicp. Показать все сообщения

четверг, 15 декабря 2011 г.

Unit-Testing SICP

Recently while reading SICP I got an idea of ​​using unit-testing techniques to perform the exercises. This is a excellent way to practice both SICP and unit-testing. Because of using MIT Scheme, my choice fell on the library "test-manager" by Alexey Radul. It is really easy to use and contains all required features)

So, let's try to use it for the exercise 1.16.
Design a procedure that evolves an iterative exponentiation process that uses successive squaring and uses a logarithmic number of steps, as does fast-expt.



; code
(define (fast-expt b n)
(define (iter a b n)
(cond
((= n 1) (* a b))
((even? n) (iter a (* b b) (/ n 2)))
(else (iter (* a b) b (- n 1)))))
(iter 1 b n))

; tests
(load "test-manager/load.scm")
(define-each-test
(assert= 1 (fast-expt 2 0))
(assert= 2 (fast-expt 2 1))
(assert= (expt 2 8) (fast-expt 2 8))
(assert= (expt 2 6) (fast-expt 2 6)))

(run-registered-tests)


Run:

mit-scheme < ex1_16.scm

1 ]=> (run-registered-tests)


Oops! I completely forgot about condition when the exponent n is zero.
Fix it:


(define (fast-expt b n)
(define (iter a b n)
(cond
((= n 0) a)
((even? n) (iter a (* b b) (/ n 2)))
(else (iter (* a b) b (- n 1)))))
(iter 1 b n))



Run it again:

1 ]=> (run-registered-tests)....

4 tests, 0 failures, 0 errors.


That's it!

среда, 14 декабря 2011 г.

Список книг по Computer Science и IT

Появилось свободное время и огромное желание чего-нибудь такого почитать, чтобы стать чуть умнее) Пока остановил свой выбор на SICP (когда-то давно смотрел видео лекции, но уже почти ничего не помню), а остальные книжки взял на заметку.

Список:
  • Don Knuth. The Art of Computer Programming
  • Structure and Interpretation of Computer Programs
  • Compilers: Principles, Techniques and Tools
  • Cormen. Introduction to algorithms
  • "Gang of Four" Design Patterns book
  • The Mythical Man-Month
  • Concepts, Techniques and Models of Computer Programming
  • Douglas Hofstadter. Gödel, Escher, Bach
  • Martin Fowler. Patterns of Enterprise Application Architecture
  • Martin Fowler. Domain Specific Languages
  • Artificial Intelligence: A Modern Approach
  • Code Complete (2nd Ed) by Steve McConnell
  • Pragmatic Programmer

Functional Programming:
  • Learn You a Haskell for Great Good
  • Types and Programming Languages by Pierce.
  • Purely Functional Data Structures by Chris Okasaki
  • The Haskell Road to Logic, Maths and Programming. Kees Doeats & Jan van Eick
  • Real World Haskell
Tanenbaum:
  • Tanenbaum. Structured Computer Organization.
  • Tanenbaum. Operating Systems: Operating System Design and Implementation
  • Tanenbaum and Wetherall. Computer Networks
  • Tanenbaum. Operating Systems
C++, C#:
  • Herb Sutter. Guru Of the Week
  • Jeffrey Richter. CLR via C#
  • Jeffrey Richter. Windows via C/C++
  • Stroustrup: The Design and Evolution of C++
  • Modern C++ Design
Ссылки