Skip to content

Instantly share code, notes, and snippets.

View kephas's full-sized avatar

Pierre Thierry kephas

  • AUTOGRIFF
  • Strasbourg, France
  • X @kephasp
View GitHub Profile
@kephas
kephas / while.lisp
Created August 5, 2011 02:12
Implementation of while in Common Lisp
; the most straightforward imperative implementation of the while
; control structure
(defmacro while1 (cond &body body)
(with-gensyms (cond-var)
`(do ((,cond-var ,cond ,cond))
((not ,cond-var))
,@body)))
; a functional implementation relying on TCO
(defmacro while2 (cond &body body)
@kephas
kephas / hunchentoot-hello.lisp
Created November 29, 2011 23:08
Hello World for Hunchentoot
(ql:quickload "hunchentoot")
(use-package :hunchentoot)
(start (make-instance 'acceptor :port 8080))
(define-easy-handler (greet :uri "/hello") (name)
(format nil "<html><body><h1>Hello ~a!</h1></body></html>" name))
@kephas
kephas / base.scm
Created December 12, 2012 00:16
Base 12
#lang racket
(define digits12 "0123456789AB")
(define digits12-list (string->list digits12))
(define (digit12->int digit)
(let loop ((digits digits12-list)
(result 0))
(if (char=? digit (first digits))
result
@kephas
kephas / while.lisp
Created March 18, 2015 21:42
While implementation in Common Lisp
(defmacro while (cond &body body)
(with-gensyms (cond-var)
`(do ((,cond-var ,cond ,cond))
((not ,cond-var))
,@body)))
@kephas
kephas / nlet.lisp
Created March 19, 2015 01:40
Example of Scheme's named let in action
(defmacro nlet (name bindings &body body)
"This is the LET from RnRS with a name, that creates a local function."
`(labels ((,name ,(mapcar #'first bindings) ,@body))
(,name ,@(mapcar #'second bindings))))
(nlet foo ((count 5))
(unless (= 0 count)
(print count)
(foo (1- count))))
@kephas
kephas / nlet.cpp
Created March 19, 2015 01:45
Scheme's named let mockup in C++
nlet foo(count = 5) {
if(count != 0) {
cout << count << endl;
foo(count--);
}
@kephas
kephas / keybase.md
Created May 27, 2015 21:41
keybase.md

Keybase proof

I hereby claim:

  • I am kephas on github.
  • I am kephasp (https://keybase.io/kephasp) on keybase.
  • I have a public key whose fingerprint is F9B7 AA0C 0DA1 EF01 E495 C5E2 C5ED 7720 D9D5 0D8A

To claim this, I am signing this object:

@kephas
kephas / padded_binary.lisp
Last active March 21, 2019 14:52
Padded binary numbers in Common Lisp
(defun read-only-chars (stream chars)
(with-output-to-string (result)
(loop while (find (peek-char nil stream) chars)
do (princ (read-char stream) result))))
(set-dispatch-macro-character #\# #\! (lambda (stream subchar arg)
(parse-integer (remove #\_ (read-only-chars stream "01_")) :radix 2)))
<?php
function creer_doublon($max) {
$start = 1;
$nombres = range(1, $max);
array_push($nombres, rand(1, $max));
shuffle($nombres);
return $nombres;
}
print_r(creer_doublon(10));
@kephas
kephas / conditions.hs
Created March 27, 2023 10:08
Alternative to early return in Functional Programming
checkUserForPromotion :: User -> Either String User
checkUserForPromotion user =
case (user.age > 18, user.country, length user.purchases > 5) of
(True, France, True) -> Right (user { status = Vip })
(False, _, _) -> Left "trop jeune"
(True, _, False) -> Left "pas assez d'achats"
(True, _, True) -> Left "pas français"