I am using the following code to get make an IMAP connection. I am want to read emails. I read this documentation
link
and could not proceed from here.
my code:
#lang racket
(define imap-server "*****")
(define imap-port-no ***)
(define username "*****")
(define pw "*****")
(define mailbox-name "INBOX")
(require openssl/mzssl
net/imap
mzlib/etc)
(define (test-connect)
(let ([c (ssl-make-client-context)])
(let-values ([(in out) (ssl-connect imap-server imap-port-no c)])
(imap-connect* in out username pw mailbox-name))))
(define-values (imap cnt recent) (test-connect))
I am getting the count of emails and count of recent mails from this. How to proceed from here. which functions i should call to read emails.
Thanks in advance.
Try something like this:
(imap-get-messages imap '(1) '(uid flags header body))
This should return a list holding the "fields" described by the flags, where header gives you the complete header part, and body is the email body. (This is just a quick experiment to see that things are working, you'll need to know which messages to retrieve, etc -- all described in the documentation.)
Here's a complete program that returns a list of the headers you want for each message in the INBOX, where each message gets an alist of the headers and their values as strings. But note that email is not really reliable for such things -- you can receive a message regardless of what appears in the To: field, and there are many other header with similar semantics (for example, Resent-To: is similar to To:, sometimes there's a Sender: header that can be more reliable than From:, etc.).
#lang racket/base
(define imap-server "imap.somewhere.com")
(define imap-port-no 1234)
(define username "----")
(define pw "----")
(define mailbox-name "INBOX")
(require racket/list openssl/mzssl net/imap net/head)
(define (test-connect)
(let ([c (ssl-make-client-context)])
(let-values ([(in out) (ssl-connect imap-server imap-port-no c)])
(imap-connect* in out username pw mailbox-name))))
(define-values [imap messages recent] (test-connect))
(define (get-interesting-headers ns)
(for/list ([x (imap-get-messages imap ns '(header))])
(filter-map
(λ (x)
(define s
(string->symbol (string-downcase (bytes->string/utf-8 (car x)))))
(and (memq s '(from to date subject))
(cons s (bytes->string/utf-8 (cdr x)))))
(extract-all-fields (car x)))))
(get-interesting-headers (for/list ([i messages]) (add1 i)))
Related
I am implementing a procedure that takes a number n and a stream, and returns a new stream with the n first elements in the streams
(define (stream-take n stream)
(cond ((stream-null? stream)
(the-empty-stream))
((= n 0) ('()))
(else
(cons (car stream)
(stream-take (- n 1)
(force (cdr stream))
)))))
(stream-take 10 nats)
this code gives the error message
=: contract violation
expected: number?
given: (2 . #<promise>)
argument position: 1st
other arguments...:
('()))
is not a stream. It should return
(the-empty-stream)
instead.
PS:
Note also, I have edited your original post and added (else ...) at the final clause.
('())) means to call the function '(), which for sure won't be a function.
SICP indicates cdr is opened up:
In section 3.5.4 , i saw this block:
(define (integral delayed-integrand initial-value dt)
(define int
(cons-stream initial-value
(let ((integrand (force delayed-integrand)))
(add-streams (scale-stream integrand dt)
int))))
int)
Normally if this was something like:
(define (stream-map proc s)
(if (stream-null? s)
the-empty-stream
(cons-stream (proc (stream-car s))
(stream-map proc (stream-cdr s)))))
The stream-cdr s would be evaluated as (cons-stream (stream-car (cdr s)) delay<>) even when the actual call would be in a delay. ie even though the stream-map function itself is delayed, the arguments are pre-computed. [Is this correct? - By the applicative model, the arguments should be substituted for ,before the function is "called", but is the call evaluation when delay is forced or when it's just specified]
Then why is let not pre-computed?
What i think? I think let is a lambda function with the variable as the arguments, so it's execution is delayed
(let ((var1 e1) (var2 e2)) e3)
is same as
Lambda (var1 var2) e3 (with var1 bound to e1 and var2 bound to e2)
Can someone please help me confirm this? Thanks
In SICP type streams the car of a stream is not delayed, but the cdr is.
The whole expression,
(let ((integrand (force delayed-integrand)))
(add-streams (scale-stream integrand dt)
int))
, is delayed since it is the second argument to cons-stream. What kind of expression is delayed doesn't matter so you can have a call, evaluation of variable or even a let there.
"the arguments are pre-computed. is this correct?"
No. (cons-stream a (func (stream-cdr b))) is just like
(cons-stream a
(lambda ()
;; our code is placed here, verbatim, as a whole:
(func (stream-cdr b))
)
)
const-stream is a macro, it just moves pieces of code around.
The lambda might be enclosed in a memo-proc call for the memoization, to do call-by-need, but it'll still have the code we wrote, like (func (stream-cdr b)), placed inside the lambda, "textually", by cons-stream. Which is a macro, just moving pieces of code around.
Regarding the snippet which you've added to the question, the authors were just being imprecise. What is meant there is, when (stream-cdr stream) in (stream-filter pred (stream-cdr stream)) will be called, it will produce (cons 10008 (delay .... )), as shown. Not before.
Where it says "which in this case is" is should have said "which in this case is the same as".
In section 3.5.1 Streams Are Delayed Lists the book says:
To make the stream implementation automatically and transparently interleave the construction of a stream with its use, we will arrange for the cdr of a stream to be evaluated when it is accessed by the stream-cdr procedure rather than when the stream is constructed by cons-stream.
I am wondering how the substitution model can be used to show certain things about infinite streams. For example, say you have a stream that puts n in the nth spot and so on inductively. I define it below:
(define all-ints
(lambda ((n <integer>))
(stream-cons n (all-ints (+ 1 n)))))
(define integers (all-ints 1))
It is pretty clear that this does what it is supposed to, but how would someone go about proving it? I decided to use induction. Specifically, induction on k where
(last (stream-to-list integers k))
provides the last value of the first k values of the stream provided, in this case integers. I define stream-to-list below:
(define stream-to-list
(lambda ((s <stream>) (n <integer>))
(cond ((or (zero? n) (stream-empty? s)) '())
(else (cons (stream-first s)
(stream-to-list (stream-rest s) (- n 1)))))))
What I'd like to prove, specifically, is the property that k = (last (stream-to-list integers k)) for all k > 1.
Getting the base case is fairly easy and I can do that, but how would I go about showing the "inductive case" as thoroughly as possible? Since computing the item in the k+1th spot requires that the previous k items also be computed, I don't know how this could be shown. Could someone give me some hints?
In particular, if someone could explain how, exactly, streams are interpreted using the substitution model, I'd really appreciate it. I know they have to be different from the other constructs a regular student would have learned before streams, because they delay computation and I feel like that means they can't be evaluated completely. In turn this would man, I think, the substitution model's apply eval apply etc pattern would not be followed.
stream-cons is a special form. It equalent to wrapping both arguments in lambdas, making them thunks. like this:
(stream-cons n (all-ints (+ 1 n))) ; ==>
(cons (lambda () n) (lambda () (all-ints (+ n 1))))
These procedures are made with the lexical scopes so here n is the initial value while when forcing the tail would call all-ints again in a new lexical scope giving a new n that is then captured in the the next stream-cons. The procedures steam-first and stream-rest are something like this:
(define (stream-first s)
(if (null? (car s))
'()
((car s))))
(define (stream-rest s)
(if (null? (cdr s))
'()
((cdr s))))
Now all of this are half truths. The fact is they are not functional since they mutates (memoize) the value so the same value is not computed twice, but this is not a problem for the substitution model since side effects are off limits anyway. To get a feel for how it's really done see the SICP wizards in action. Notice that the original streams only delayed the tail while modern stream libraries delay both head and tail.
Well, I am trying to get some things in racket, I am currently studying streams. I try to create a function that will edit a stream, for example add a pair of (int, element) in place of each stream's element.
For example initial_stream : <1,2,3, …>
edited_stream : <(int . 1) (int . 2) (int . 3) ….. >
I wrote this but it seems to enter an endless loop (with int=>13)
(define (stream-add-zero s)
(cons (cons 13 (car (s))) (stream-add-zero (cdr (s)))))
Thanks in advance.
If by "stream" you mean Racket's lazy stream data structure, this can be done with an application of stream-map.
(define initial-stream (in-naturals))
(define edited-stream (stream-map (λ (i) (cons 'int i)) initial-stream))
;; sanity check
(require rackunit)
(check-equal? (stream-ref edited-stream 3) '(int . 3))
This is assuming the int in your edited_stream was a symbol.
Just started with Scheme. I'm having problem with printing on console.
A simple list printing example:
(define factorial
(lambda (n)
(cond
((= 0 n) 1)
(#t (* n (factorial (- n 1)))))))
I want to print n, every time the function is called. I figured that I can't do that within the same function? Do I need to call another function just so I can print?
Printing in Scheme works by calling display (and possibly, newline).
Since you want to call it sequentially before/after something else (which, in a functional (or in the case of Scheme, functional-ish) language only makes sense for the called functions side-effects), you would normally need to use begin, which evaluates its arguments in turn and then returns the value of the last subexpression. However, lambda implicitly contains such a begin-expression.
So in your case, it would go like this:
(lambda (n)
(display n) (newline)
(cond [...]))
Two remarks:
You can use (define (factorial n) [...]) as a shorthand for (define factorial (lambda (n) [...])).
The way you implement factorial forbids tail call-optimization, therefore the program will use quite a bit of stack space for larger values of n. Rewriting it into a optimizable form using an accumulator is possible, though.
If you only want to print n once, when the user calls the function, you will indeed need to write a wrapper, like this:
(define (factorial n)
(display n) (newline)
(inner-factorial n))
And then rename your function to inner-factorial.