Skip to content

Instantly share code, notes, and snippets.

View belmarca's full-sized avatar

Marc-André Bélanger belmarca

View GitHub Profile
@belmarca
belmarca / Makefile
Created July 11, 2018 23:31
Calling Fortran from C using iso_c_binding
all:
gcc -c sum_to_n.c
gfortran sum_to_n.f90 sum_to_n.o -o sum
@belmarca
belmarca / Makefile
Last active July 16, 2018 22:25
Calling Fortran from Gambit-C
gambit:
gfortran -c sum_to_n.f90 -o sum_to_n.o
gsc -ld-options sum_to_n.o sum_to_n.scm
clean:
rm sum_to_n.o*
@belmarca
belmarca / macro.scm
Created August 9, 2019 00:23
Exemple de macro scheme.
(define-syntax de
(syntax-rules (à)
((de x à y) (cond ((> x y) (let loop ((z y) (l '()))
(cond ((< z x) (loop (+ z 1) (cons z l)))
((= z x) (cons z l)))))
((= x y) x)
((< x y) (let loop ((z y) (l '()))
(cond ((> z x) (loop (- z 1) (cons z l)))
((= z x) (cons z l)))))))))
(def (u8vector->uint bv (guard #t))

  (def (compute bv)
    (let ((l (fx- (u8vector-length bv) 1)))
      (let loop ((s 0) (i 0))
        (if (< i l)
          (loop (+ s (* (u8vector-ref bv i) (expt 256 (- l i)))) (+ i 1))
          (+ s (* (u8vector-ref bv i) (expt 256 (- l i))))))))
gxi -:d2- -e '(define x (make-u8vector 10000000))(let loop () (loop))'
*** GC: 967us, 2.1M alloc, 5.0M heap, 858K live (17% 874512+4512)
*** GC: 753us, 3.3M alloc, 7.0M heap, 1.8M live (25% 1785408+49800)
*** GC: 864us, 5.5M alloc, 8.1M heap, 2.4M live (29% 2411696+70120)
*** GC: 849us, 7.2M alloc, 30M heap, 12M live (42% 2807472+10147656)
*** GC: 839us, 20M alloc, 30M heap, 12M live (42% 2807584+10147656)
*** GC: 970us, 33M alloc, 30M heap, 12M live (42% 2807584+10147656)
*** GC: 813us, 45M alloc, 30M heap, 12M live (42% 2807584+10147656)
*** GC: 913us, 58M alloc, 30M heap, 12M live (42% 2807584+10147656)
*** GC: 846us, 71M alloc, 30M heap, 12M live (42% 2807584+10147656)
(time (u8vector->uint16-0 u16))
0.000004 secs real time
0.000005 secs cpu time (0.000004 user, 0.000001 system)
no collections
64 bytes allocated
no minor faults
no major faults
(time (std/misc/bytes#u8vector->uint u16))
0.000004 secs real time
0.000005 secs cpu time (0.000005 user, 0.000000 system)
@belmarca
belmarca / reverse-me.ss
Last active August 20, 2019 14:16
Gerbil scheme reverse syntax macro
(defsyntax (reverse-me stx)
(syntax-case stx ()
((macro . args)
(with-syntax ()
(datum->syntax #'macro (reverse (syntax->datum #'args)))))))
;; > (reverse-me "backwards" "am" "I" values)
;; "I"
;; "am"
;; "backwards"
@belmarca
belmarca / predlambda.ss
Last active August 23, 2019 03:30
predicate lambda
(begin-syntax
(def (split-parg stx)
(datum->syntax stx
(stx-map string->symbol
(string-split (symbol->string (stx-e stx)) #\:))))
(def (stx-cadr stx)
(stx-car (stx-cdr stx))))
(defsyntax (predlambda stx)
(import :std/misc/text
:std/misc/list
:std/pregexp
:std/iter
:std/srfi/13
:std/format
:std/sugar)
;; TODO: Add module level definition for the default delimiter
;; to avoid repitition and facilitate maintenance.
@belmarca
belmarca / quasistring.ss
Created September 19, 2019 02:47
gerbil quasistring
;; quasistring.ss
(import :std/format
:std/net/request)
;; variant: return procedure of n args?
(defsyntax (quasistring stx)
(syntax-case stx ()
((macro s)
(stx-string? #'s)