I'd like to hash stdin in chunks using racket but can't find an equivalent to Python's hashlib's sha1.update function is there an equivalent or alternative?
Here is one way to do it:
#lang racket
(require (planet soegaard/digest:1:2/digest))
(define a #"The quick brown fox jumps ")
(define b #"over the lazy dog")
(define a+b (bytes-append a b))
(digest a+b 'sha1)
(define c (make-digest-context 'sha1))
(update-context c a)
(update-context c b)
(final-context->hex-string c)
The output is:
"2fd4e1c67a2d28fced849ee1bb76e7391b93eb12"
"2fd4e1c67a2d28fced849ee1bb76e7391b93eb12"
You can find documentation on the digest collection here:
http://planet.racket-lang.org/package-source/soegaard/digest.plt/1/2/doc.txt
Use it something like this:
(define c (make-digest-context 'sha1))
(define in (open-input-file "foo"))
(let loop ()
(define chunk (read-bytes 4096 in))
(cond [(eof-object? chunk) (final-context->hex-string c)]
[else (update-context c chunk)
(loop)]))
Take a look at Greg Hendershott's sha library, which adds Racket support for SHA-1 and SHA-2 by providing a Racket interface to the OpenSSL implementation of SHA-1 and SHA-2.
(require sha)
(sha1 bstr) -> sha1?
bstr : bytes?
Although it doesn't have an update() procedure, as stated in Python's own documentation a call like this:
m.update(a); m.update(b)
Is equivalent to this:
m.update(a+b)
Related
Is it possible to define a macro that has same name as a function in racket, but not override it. (So that name would have 2 possible usage, depending on the passed argument)
For example function round.
1. usage (racket function)
( round 1.2 )
-> 1
2. usage (our macro)
(round (someStruct arg))
-> resultStruct
Using function
(require (only-in racket [round #round]))
(define (my-round v)
;; do whatever you want to do here
"hello")
(define (round v)
(cond
[(number? v) (#round v)]
[else (my-round v)]))
(define x 1.2)
(round x) ;=> 1.0
(define y "abc")
(round y) ;=> "hello"
By defining round as a function, the case analysis is done on the value that is passed into the function at runtime.
Using macro
(require syntax/parse/define
(only-in racket [round #round]))
(define-syntax-parser round
[(_ x:number) #'(#round x)]
[(_ x) #'"hello"])
(define x 1.2)
(round x) ;=> "hello"
(define y "abc")
(round y) ;=> "hello"
(round 1.2) ;=> 1.0
(round (this is not a sensible expression but it works (()))) ;=> "hello"
By defining round as a macro, the case analysis is done on the syntax fragment that is passed into the macro at compile-time. In the above example, we will use the actual Racket's round when the operand of the macro is a literal number. Everything else will be transformed to "hello". Note that at compile-time, identifiers like x is not evaluated and associated to a value yet. The only thing you know is that it is an identifier, which is not a literal number, so it transforms to "hello". This program after macro expansion will roughly be:
(require syntax/parse/define
(only-in racket [round #round]))
(define x 1.2)
"hello"
(define y "abc")
"hello"
(#round 1.2)
"hello"
Choose which one you prefer. I suspect that you actually want to use function rather than macro.
I'm trying to experiment with what I can do in Racket, and I want to suffix numbers with letters.
For this example, I'd simply like to represent 10000 as 10K, and 1000000 as 1M.
Is there way (with macros or otherwise) that I can expand 1M to:
(* 1 1000000)
Or something to that effect?
In Racket, things like 10K are identifiers, which normally would refer to variables. There are two ways to make them into numbers:
1: redefine what "undefined" identifiers mean
You can redefine what to do on an undefined identifier by defining a #%top macro.
#lang racket
(require syntax/parse/define
(only-in racket [#%top old-#%top]))
(define-syntax-parser #%top
[(_ . x:id)
#:when (id-has-a-k-at-the-end? #'x)
(transform-id-into-number #'x)]
[(_ . x)
#'(old-#%top . x)])
However, this has a subtle problem. If there are any identifiers or variables in your program with K's on the end, they could override any numbers that were written that way. You would need to be careful not to accidentally override something that was intended to be a number.
2: make a reader extension that turns them into numbers instead of identifiers
This will take more time, but it's closer to the "right way" to do this, since it avoids conflicts when variables happen to have K's on the end.
One of the easier ways to extend the reader is with a readtable. You can make a function that extends a readtable like this:
;; Readtable -> Readtable
(define (extend-readtable orig-rt)
;; Char InputPort Any Nat Nat Nat -> Any
(define (rt-proc char in src ln col pos)
....)
...
(make-readtable orig-rt
#f 'non-terminating-macro rt-proc
...))
To use this to define a #lang language, you need to put the reader implementation in your-language/lang/reader.rkt. Here that's number-with-k/lang/reader.rkt, where the number-with-k directory is installed as a single-collection package (raco pkg install path/to/number-with-k).
number-with-k/lang/reader.rkt
#lang racket
(provide (rename-out [-read read]
[-read-syntax read-syntax]
[-get-info get-info]))
(require syntax/readerr
syntax/module-reader)
;; Readtable -> Readtable
(define (extend-readtable orig-rt)
;; Char InputPort Any Nat Nat Nat -> Any
(define (rt-proc char in src ln col pos)
....)
...
(make-readtable orig-rt
#f 'non-terminating-macro rt-proc))
;; [X ... -> Y] -> [X ... -> Y]
(define ((wrap-reader rd) . args)
(parameterize ([current-readtable (extend-readtable (current-readtable))])
(apply rd args)))
(define-values [-read -read-syntax -get-info]
(make-meta-reader 'number-with-k
"language path"
lang-reader-module-paths
wrap-reader
wrap-reader
identity))
The main work goes into filling in the .... holes in the extend-readtable function. For example, you can make it recognize identifiers that end with K like this:
;; Readtable -> Readtable
(define (extend-readtable orig-rt)
;; Char InputPort Any Nat Nat Nat -> Any
(define (rt-proc char in src ln col pos)
(define v (read-syntax/recursive src in char orig-rt #f))
(cond
[(and (identifier? v) (id-has-a-k-at-the-end? v))
(transform-id-into-number v)]
[else
v]))
(make-readtable orig-rt
#f 'non-terminating-macro rt-proc))
Once this is done, and you have the number-with-k directory installed as a package, you should be able to use #lang number-with-k like this:
#lang number-with-k racket
(+ 12K 15)
; => 12015
The simplest to is to define the suffixes you need.
(define K 1000)
(define M 1000000)
Then write (* 3.14 M) for 3.14 millions.
As others mention, Racket supports scientific notation 3.14E6 is also 3.14 million.
Yet another alternative is to define functions K, M etc like:
(define (M x) (* x 1000000))
Then you can write
(M 3.14)
to mean 3.14 million.
Racket does already have built in support for this, kind of, via scientific notation:
1e6 ; 1000000.0 ("1M")
2e7 ; 20000000.0
In Common Lisp it is relatively easy to create a macro-defining macro. For example, the following macro
(defmacro abbrev (short long)
`(defmacro ,short (&rest args)
`(,',long ,#args)))
is a macro-defining macro, because it expands to another macro.
If we now put
(abbrev def defun)
in our program, we can write def instead of defun whenever we define a new function.
Of course, abbrev can be used for other things, too. For example, after
(abbrev /. lambda)
we can write (/. (x) (+ x 1)) instead of (lambda (x) (+ x 1)). Nice. (For detailed explanation of abbrev, see http://dunsmor.com/lisp/onlisp/onlisp_20.html)
Now, my questions are:
Can I write the macro-defining macros in Racket?
If I can, how to do that? (for example, how to write something similar to
abbrev macro in Racket?)
According to this part of the Racket Guide:
(define-syntax-rule (abbrev short long)
(define-syntax-rule (short body (... ...))
(long body (... ...))))
Quoting the above link:
The only non-obvious part of its definition is the (... ...), which
“quotes” ... so that it takes its usual role in the generated macro,
instead of the generating macro.
Now
(abbrev def define)
(abbrev /. lambda)
(def f (/. (x) (+ x 1)))
(f 3)
yields
4
FWIW, it works on Guile as well, so it's no Racket-specific thing.
ad 1. Yes.
ad 2. Your example can most easily be written
#lang racket
(define-syntax (abbrev stx)
(syntax-case stx ()
[(_ short long)
#'(define-syntax short (make-rename-transformer #'long))]))
(abbrev def define)
(def x 42)
x
The example above evaluates to 42.
I find that renaming can be done simply with define or let statements:
(define =? =)
(define lr list-ref)
or:
(let ((=? =)
(lr list-ref))
(println (lr '(1 2 3) 2))
(println (=? 1 2))
(println (=? 1 1)))
Output:
3
#f
#t
There seem to be no need for any macro for this purpose.
I have a set of derivation rules implemented in racket. We can assume that there aren't any optional , which means there are no rules containing pipes (in BNF): ::= |
In racket, I have got something like this:
(define *rules*
'((S . ("b" "a"))
(B . ("a"))
(C . (S B))))
Please note that terminal symbols are implemented in the form of racket strings, nonterminal symbols in the form of racket symbols. Now, I'd like to import this rules from another racket file containing rules in backus naur syntax:
S ::= ba
B ::= a
C ::= SB
(capital letter = nonterminal)
Therefore, I need to extend the racket syntax. I have no idea how to handle that. Can you help me? It shouldn't be that much code ...
I think you're looking to parse a file that's written using the BNF syntax, and produce an s-expression version; is that right?
If so, it shouldn't be hard. In particular, the format implied by your question is that every line is of the form
<NT> :: = [<NT>|<T>]*
... which you could take apart like this:
#lang racket
;; COPYRIGHT 2012 John B. Clements (clements#brinckerhoff.org)
;; Licensed under the Apache License, version 2.
;; (You're free to use it, but your source code has to include
;; my authorship.)
(require rackunit)
(define example
(list "S ::= ba"
"B ::= a"
"C ::= SB"))
;; parse a single line:
;; string -> (list/c symbol? (listof (or/c string? symbol?)))
(define (parse-line l)
(match (regexp-match #px"^([A-Z]) ::= ([A-Za-z]*)$")
[(list _ lhs rhses)
(list lhs (map parse-char (string->list rhses)))]))
;; parse a single char:
;; char -> (or/c symbol? string?)
(define (parse-char ch)
.. oops! out of time. You'll have to write this part yourself... )
(check-expect (map parse-line example)
'((S ("b" "a"))
(B ("a"))
(C (S B))))
Oops! I see a bug in there. No problem, you'll figure it out. Gotta run....
Is it possible to use/implement tacit programming (also known as point-free programming) in Lisp? And in case the answer is yes, has it been done?
This style of programming is possible in CL in principle, but, being a Lisp-2, one has to add several #'s and funcalls. Also, in contrast to Haskell for example, functions are not curried in CL, and there is no implicit partial application. In general, I think that such a style would not be very idiomatic CL.
For example, you could define partial application and composition like this:
(defun partial (function &rest args)
(lambda (&rest args2) (apply function (append args args2))))
(defun comp (&rest functions)
(flet ((step (f g) (lambda (x) (funcall f (funcall g x)))))
(reduce #'step functions :initial-value #'identity)))
(Those are just quick examples I whipped up – they are not really tested or well thought-through for different use-cases.)
With those, something like map ((*2) . (+1)) xs in Haskell becomes:
CL-USER> (mapcar (comp (partial #'* 2) #'1+) '(1 2 3))
(4 6 8)
The sum example:
CL-USER> (defparameter *sum* (partial #'reduce #'+))
*SUM*
CL-USER> (funcall *sum* '(1 2 3))
6
(In this example, you could also set the function cell of a symbol instead of storing the function in the value cell, in order to get around the funcall.)
In Emacs Lisp, by the way, partial application is built-in as apply-partially.
In Qi/Shen, functions are curried, and implicit partial application (when functions are called with one argument) is supported:
(41-) (define comp F G -> (/. X (F (G X))))
comp
(42-) ((comp (* 2) (+ 1)) 1)
4
(43-) (map (comp (* 2) (+ 1)) [1 2 3])
[4 6 8]
There is also syntactic threading sugar in Clojure that gives a similar feeling of "pipelining":
user=> (-> 0 inc (* 2))
2
You could use something like (this is does a little more than -> in
Clojure):
(defmacro -> (obj &rest forms)
"Similar to the -> macro from clojure, but with a tweak: if there is
a $ symbol somewhere in the form, the object is not added as the
first argument to the form, but instead replaces the $ symbol."
(if forms
(if (consp (car forms))
(let* ((first-form (first forms))
(other-forms (rest forms))
(pos (position '$ first-form)))
(if pos
`(-> ,(append (subseq first-form 0 pos)
(list obj)
(subseq first-form (1+ pos)))
,#other-forms)
`(-> ,(list* (first first-form) obj (rest first-form))
,#other-forms)))
`(-> ,(list (car forms) obj)
,#(cdr forms)))
obj))
(you must be careful to also export the symbol $ from the package in
which you place -> - let's call that package tacit - and put
tacit in the use clause of any package where you plan to use ->, so -> and $ are inherited)
Examples of usage:
(-> "TEST"
string-downcase
reverse)
(-> "TEST"
reverse
(elt $ 1))
This is more like F#'s |> (and the shell pipe) than Haskell's ., but they
are pretty much the same thing (I prefer |>, but this is a matter of personal taste).
To see what -> is doing, just macroexpand the last example three times (in SLIME, this is accomplished by putting the cursor on the first ( in the example and typing C-c RET three times).
YES, it's possible and #danlei already explained very well. I am going to add up some examples from the book ANSI Common Lisp by Paul Graham, chapter 6.6 on function builders:
you can define a function builder like this:
(defun compose (&rest fns)
(destructuring-bind (fn1 . rest) (reverse fns)
#'(lambda (&rest args)
(reduce #'(lambda (v f) (funcall f v))
rest
:initial-value (apply fn1 args)))))
(defun curry (fn &rest args)
#'(lambda (&rest args2)
(apply fn (append args args2))))
and use it like this
(mapcar (compose #'list #'round #'sqrt)
'(4 9 16 25))
returns
((2) (3) (4) (5))
The compose function call:
(compose #'a #'b #'c)
is equlvalent to
#'(lambda (&rest args) (a (b (apply #'c args))))
This means compose can take any number of arguments, yeah.
Make a function which add 3 to argument:
(curry #'+ 3)
See more in the book.
Yes, this is possible in general with the right functions. For example, here is an example in Racket implementing sum from the Wikipedia page:
#lang racket
(define sum (curry foldr + 0))
Since procedures are not curried by default, it helps to use curry or write your functions in an explicitly curried style. You could abstract over this with a new define macro that uses currying.