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 15:29

Be careful with learning languages. I wasted a lot of time on Haskell, only to have become a worse programmer. Same with C++.

I can't see how a regular person can master more than 3 programming languages in their lifetime, unless they devote their whole lives to programming, in which case you may be able to master 6. But you should not do this. By master I mean master - know the ins and outs of a number of existing implementations, be able to implement it yourself, know all the tools and libraries, know exactly how the language differs with it's siblings, be able to express anything written in any language in this one, have experience with the language in a wide number of contexts and so on. So choose wisely. You can dabble in many languages, but you can't master them.

Erik Naggum programmed in Common Lisp and Ada and had mastered C before that. This is a nice combination. I myself am currently mastering Common Lisp.

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