Name: Anonymous 2015-03-09 6:23
Please post some Forth programs hear.
Also Lisp and C and Lua are welcome.
NO HASKELL
Also Lisp and C and Lua are welcome.
NO HASKELL
; Drakma and CL-PPCRE are required
(defparameter *watch-list* nil)
(defparameter *list-path* "/foo/bar/4-ch.watchlist"))
(defun watch-thread (url)
"Add the thread denoted by the url
or id to the watch list."
(dump
(let ((info (get-thread-info url)))
(unless (assoc (car info) *watch-list*)
(push info *watch-list*)))))
(defun extract-id (url)
(parse-integer (aref (nth-value 1 (cl-ppcre:scan-to-strings "pl/(.*)"
url)) 0)
:junk-allowed t))
(defun get-thread-info (url)
"Return a list with
CAR, thread ID
CADR, thread title
CADDR, board name
CADDDR, thread postcount
CADDDDR, thread link"
(let* ((text (aref (nth-value 1
(cl-ppcre:scan-to-strings "<h2>(.*)</h2>(.*)"
(drakma:http-request url)))
0))
(title (aref (nth-value 1
(cl-ppcre:scan-to-strings "(.*) <small>"
text))
0))
(postcount (aref (nth-value 1
(cl-ppcre:scan-to-strings "<small>.(.*).</small>"
text))
0))
(id (extract-id url))
(board (aref (nth-value 1
(cl-ppcre:scan-to-strings "4-ch.net/(.*)/kareha"
url))
0)))
(list id title board (parse-integer postcount)
(format nil "http://4-ch.net/~A/kareha.pl/~D"
board id))))
(defun unwatch-thread (id)
"Remove a thread from the watch list."
(setq *watch-list*
(delete id *watch-list* :key #'car)))
(defun update-list (&optional (list *watch-list*))
"Update all threads and print information of the new threads."
(dolist (x list)
(let ((new-info (get-thread-info
(format nil "http://4-ch.net/~A/kareha.pl/~D"
(caddr x)
(car x)))))
(if (> (cadddr new-info)
(cadddr x))
(progn
(setf (car x) (car new-info)
(cadr x) (cadr new-info)
(caddr x) (caddr new-info)
(cadddr x) (cadddr new-info)
(car (cddddr x)) (car (cddddr new-info)))
(dump (list x)))))
list))
(defun dump (&optional (list *watch-list*))
(format t "~{~{ID: ~D~%Title: ~A~%Board: ~A~%Postcount: ~D~%Link: ~A~%~%~}~}"
list)
(defun save-watch-list (&optional (path *list-path*)
(list *watch-list*))
(with-open-file (stream path :if-exists :supersede :direction :output)
(write list :stream stream)))
(defun load-watch-list (&optional (path *list-path*))
(with-open-file (stream path)
(dump (setq *watch-list*
(read stream)))))
-- static Peano constructors and numerals
data Zero
data Succ n
type One = Succ Zero
type Two = Succ One
type Three = Succ Two
type Four = Succ Three
-- dynamic representatives for static Peanos
zero = undefined :: Zero
one = undefined :: One
two = undefined :: Two
three = undefined :: Three
four = undefined :: Four
-- addition, a la Prolog
class Add a b c | a b -> c where
add :: a -> b -> c
instance Add Zero b b
instance Add a b c => Add (Succ a) b (Succ c)
-- multiplication, a la Prolog
class Mul a b c | a b -> c where
mul :: a -> b -> c
instance Mul Zero b Zero
instance (Mul a b c, Add b c d) => Mul (Succ a) b d
-- factorial, a la Prolog
class Fac a b | a -> b where
fac :: a -> b
instance Fac Zero One
instance (Fac n k, Mul (Succ n) k m) => Fac (Succ n) m
-- try, for "instance" (sorry):
--
-- :t fac four