Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

lisp change machine

Name: Anonymous 2015-09-19 12:52

dynamic programming mixed with HOFs. originally an exam question. brain on fire for an undergrad fuckwit like myself

(define (change amt coins)
(cond
((= amt 0) '(()))
((or (<= amt 0) (null? coins)) '())
(else
(fold-right (lambda (a b) (append a b))
'()
(map
(lambda (n)
(map
(lambda (u)
(append (list n) u))
(change (- amt n)
(filter (lambda (k) (>= k n)) coins))))
coins)))))

;; -----------------------------------------------------------------------

(define (display-nl u)
(display u)
(newline))

(define display-elements
(lambda (u)
(map display-nl u)))

(define (demo r k)
(display-nl '-----------------------------)
(display r) (display ';;) (display k) (newline)
(display-nl '=============================)
(display-elements (change r k))
(display-nl '-----------------------------)
(newline))

;; -----------------------------------------------------------------------

(newline)

(demo 5 '(1 2 5))
(demo 13 '(1 3 5 7))
$ ./lisp <change.lisp

Loaded `prefix.txt'

-----------------------------
5;;(1 2 5)
=============================
(1 1 1 1 1)
(1 1 1 2)
(1 2 2)
(5)
-----------------------------

-----------------------------
13;;(1 3 5 7)
=============================
(1 1 1 1 1 1 1 1 1 1 1 1 1)
(1 1 1 1 1 1 1 1 1 1 3)
(1 1 1 1 1 1 1 1 5)
(1 1 1 1 1 1 1 3 3)
(1 1 1 1 1 1 7)
(1 1 1 1 1 3 5)
(1 1 1 1 3 3 3)
(1 1 1 3 7)
(1 1 1 5 5)
(1 1 3 3 5)
(1 3 3 3 3)
(1 5 7)
(3 3 7)
(3 5 5)
-----------------------------

Name: Anonymous 2015-09-23 14:39

Other advice:

Don't be seduced by trends. Use old languages and old technology. The state of the art hasn't caught up to the 1980s yet, so you're not missing out on anything.

The best libraries are the libraries that haven't been updated for 10 years, and work with no hiccups after a trivial install.

An example of a trend id all this hooh-ah over dependent types - the example they like to point to is that you can have the length of a vector as part of the type. Well you could declare such a type in Common Lisp since CLtL1, and compilers like SBCL do something about it.

Another example is OpenGL. After spending the last 10 years butchering a nice high level API for 3D graphics we've wound up with some low level bullshit mixed with nonsensical abstractions. You need a team of >4 programmers to get anything done, and code bases end up huge. It's becoming a compilation target because it is no longer habitable. OpenGL 1.5, on the other hand, is absolutely beautiful, and an OpenGL 1.5 program reads like a book.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List