I'm using vim, tslime, and tmux to send scheme code to the racket repl (v6.7) via the command line. I'm running on Arch Linux.
When I do so, I commonly get the auto-completion warning:
"Display all [xyz] possibilities (y or n)?"
I'm guessing racket is interpreting the tabs in the snippets of code tmux is sending from vim.
Here's my problem:
1. Annoying. How do I get rid of it?
2. Code needs to be passed twice! Some code only works properly if I pass it to the racket repl twice in a row. In DrRacket the code works perfectly, so obviously the tabs are preventing the repl from evaluating some s-expressions (and passing the code twice resolves it somehow).
Here is the code, from SICP, 1.2.6:
; Smallest divisor
(define (smallest-divisor n) (find-divisor n 2))
(define (find-divisor n test-divisor)
(cond ((> (square test-divisor) n) n) ; tests as far as sqrt of n
((divides? test-divisor n) test-divisor)
(else (find-divisor n (+ test-divisor 1)))))
(define (divides? a b) (= (remainder b a) 0))
(define (square x) (* x x))
(define (prime? n)
(= n (smallest-divisor n)))
Here is what this all looks like in the racket repl. Notice how in the first pass below, the the procedure prime? evaluates (incorrectly) that 10 is prime.
Welcome to Racket v6.7.
> (define (smallest-divisor n) (find-divisor n 2))
> (define (find-divisor n test-divisor)
(cond ((> (square test-divisor) n) n) Display all 254 possibilities? (y or n)
(cond ((> (square test-divisor) n) n) Display all 254 possibilities? (y or n)
(cond ((> (square test-divisor) n) n) ; tests as far as sqrt of n
Display all 254 possibilities? (y or n) (else (find-divisor n (+
test-divisor 1)))))
> (define (divides? a b) (= (remainder b a) 0))
> (define (square x) (* x x))
> (define (prime? n)
(= n (smallest-divisor n)))
> (prime? 10)
#t
Passing the code a second time, we get the following output. And this time, prime? correctly evaluates that 10 is not prime.
> (define (smallest-divisor n) (find-divisor n 2))
> (define (find-divisor n test-divisor)
(cond ((> (square test-divisor) n) n) Display all 258 possibilities? (y or n)
(cond ((> (square test-divisor) n) n) Display all 258 possibilities? (y or n)
(cond ((> (square test-divisor) n) n) ; tests as far as sqrt of n
Display all 258 possibilities? (y or n) ((divides? test-divisor n)
test-divisor)
Display all 258 possibilities? (y or n) (else (find-divisor n (+
test-divisor 1)))))
> (define (divides? a b) (= (remainder b a) 0))
> (define (square x) (* x x))
> (define (prime? n)
(= n (smallest-divisor n)))
> (prime? 10)
#f
>
Related
Consider the following β-reductor:
(define (normalize v)
(set! count 0)
(set! reflected '())
(reify v))
(define (reify v)
(if (memq v reflected)
(v cancel)
(let ((x (gensym)))
(ABS x (reify (v (reflect x)))))))
(define (reflect e)
(let ((f (lambda (v)
(if (eq? v cancel)
e
(reflect (APP e (reify v)))))))
(set! reflected (cons f reflected))
f))
(define (APP e1 e2) `(,e1 ,e2))
(define (ABS x e) `(lambda (,x) ,e))
(define reflected '())
(define count 0)
(define cancel '(cancel))
(define (gensym)
(set! count (+ 1 count))
(string->symbol (string-append "x" (number->string count))))
I would like to analyze its β-reduction order. However, since I'm not too savvy with Scheme, I would like to see the intermediate terms (right now it only prints the end result) it calculates as pure lambda expressions. I know how to display a line, but I am unable to squeeze a (display term) (newline) in the right spot.
Below are two simple terms that can be used to verify a solution - Church one (λfx.f x) and succ (λnfx.f (n f x)) (I hope I wrote them correctly in Scheme):
(define One
(lambda (f) (lambda (x) (f x))))
(define succ
(lambda (n) (lambda (f) (lambda (x) (f ((n f) x))))))
(normalize (succ One))
Is it possible to display the intermediate terms calculated by this reductor?
No, This is a big-step NBE algorithm (meaning "all at once"). it works by reflecting your term language into the host languages to piggy back on the hosts execution engine.
I am working on a homework assignment, and the first functions had me doing things like deleting a given element of a list, or displaying a given element of a list. The next functions want me to delete nested lists, or display them. Do you have any general tips for working with nested lists? I imagine the functions will be very similar to the ones I wrote before, just tweaked a bit.
Here are two example of functions I have written so far. Note I must use the "cond" style of writing functions.
(defun delnth(L A)
(cond ((= A 1) (rest L))
(t (cons (first L) (delnth (rest L) (- A 1))))))
(defun remv(A L)
(cond ((eq A (first L)) (remv A (rest L)))
((null L) nil)
(t (cons (first L) (remv A (rest L) )))))
Both of your functions work with nested cons. '(1 2 3) is (1 . (2 . (3 . ())) and (cons 1 (cons 2 (cons 3 '()))). A nested list would be that you also have nested cons in the car of cons. eg. ((1 . ()) . ()) ; ==> ((1)).
It's important to be able to see ((1) (2 3)) and understand that it is ((1 . ()) . ((2 . (3 . ())) . ())) so that if you want to access 3 it's obviously the path d,a,d,a and in the reverse you can construct the accessor cadadr.
cons with cons in the car part are trees. When you make a function that walks the tree you need to walk both the car and the cdr in the event they are cons. Thus:
;; generic function to walk trees and reduce
;; by combiner every value accessed by term
;; the default argument to combiner is tree-null
(defun accumulate-tree (tree term combiner null-value)
(labels ((rec (tree)
(cond ((null tree) null-value)
((atom tree) (funcall term tree))
(t (funcall combiner (rec (car tree))
(rec (cdr tree)))))))
(rec tree)))
(defun sum-numbers (tree)
(accumulate-tree tree
#'identity
#'+
0))
(defun count-numbers (tree)
(accumulate-tree tree
(lambda (v) (if (numberp v) 1 0))
#'+
0))
(sum-numbers '((1) (2 3) (((4)) 5))) ; ==> 15
(count-numbers '(a b 4 3 (e f 5) . 9)) ; ==> 4
The combiner doesn't need to reduce to an atom:
(defun reverse-tree (tree)
(accumulate-tree tree
#'identity
(lambda (a d) (cons d a))
'()))
(reverse-tree '((1 2) (3 4)))
; ==> ((nil (nil . 4) . 3) (nil . 2) . 1)
I'm starting to learn Emacs Lisp and as an exercise I'm trying to implement map using foldr. The code is the following:
(defun foldr (f z l)
(if (null l)
z
(funcall f (car l)
(foldr f z
(cdr l)))))
(defun map (f l)
(foldr (lambda (x z)
(cons (funcall f x) z))
nil
l))
However, when I try to evaluate, for example,
(map (lambda (x) (+ x 1)) '(1 2 3 4 5))
in Emacs with eval-last-sexp (after evaluating both foldr and map), the result is the following:
Debugger entered--Lisp error: (wrong-number-of-arguments (lambda (x z) (cons (funcall f x) z)) 1)
(lambda (x z) (cons (funcall f x) z))(5)
funcall((lambda (x z) (cons (funcall f x) z)) 5)
(cons (funcall f x) z)
(lambda (x z) (cons (funcall f x) z))(5 nil)
funcall((lambda (x z) (cons (funcall f x) z)) 5 nil)
(if (null l) z (funcall f (car l) (foldr f z (cdr l))))
foldr((lambda (x z) (cons (funcall f x) z)) nil (5))
(funcall f (car l) (foldr f z (cdr l)))
(if (null l) z (funcall f (car l) (foldr f z (cdr l))))
foldr((lambda (x z) (cons (funcall f x) z)) nil (4 5))
(funcall f (car l) (foldr f z (cdr l)))
(if (null l) z (funcall f (car l) (foldr f z (cdr l))))
foldr((lambda (x z) (cons (funcall f x) z)) nil (3 4 5))
(funcall f (car l) (foldr f z (cdr l)))
(if (null l) z (funcall f (car l) (foldr f z (cdr l))))
foldr((lambda (x z) (cons (funcall f x) z)) nil (2 3 4 5))
(funcall f (car l) (foldr f z (cdr l)))
(if (null l) z (funcall f (car l) (foldr f z (cdr l))))
foldr((lambda (x z) (cons (funcall f x) z)) nil (1 2 3 4 5))
map((lambda (x) (+ x 1)) (1 2 3 4 5))
eval((map (function (lambda (x) (+ x 1))) (quote (1 2 3 4 5))) nil)
eval-last-sexp-1(nil)
#[257 "\204\303!\207 \303!\n)B\211A =\204\211A\211#\207" [eval-expression-debug-on-error eval-last-sexp-fake-value debug-on-error eval-last-sexp-1] 4 2471606 "P"](nil)
ad-Advice-eval-last-sexp(#[257 "\204\303!\207 \303!\n)B\211A =\204\211A\211#\207" [eval-expression-debug-on-error eval-last-sexp-fake-value debug-on-error eval-last-sexp-1] 4 2471606 "P"] nil)
apply(ad-Advice-eval-last-sexp #[257 "\204\303!\207 \303!\n)B\211A =\204\211A\211#\207" [eval-expression-debug-on-error eval-last-sexp-fake-value debug-on-error eval-last-sexp-1] 4 2471606 "P"] nil)
eval-last-sexp(nil)
call-interactively(eval-last-sexp nil nil)
command-execute(eval-last-sexp)
I don't understand why this doesn't work, when the following super similar implementation in Haskell works fine:
foldr f z l = if (null l)
then z
else f (head l) (foldr f z (tail l))
map f l = foldr (\ x z -> (:) (f x) z) [] l
So why aren't the Lisp and Haskell programs equivalent? What's the nature of the problem in the Lisp implementation, i.e., why isn't map working?
(setq lexical-binding t)
f is a free variable in the lambda form you pass to foldr.
With lexical binding, your example works fine:
(map (lambda (x) (+ x 1)) '(1 2 3 4 5)) ; ==> (2 3 4 5 6)
I just started learning scheme. I'm trying to make a procedure to look for an item in a trinary tree. It returns true if it is found.
Here is what I got so far:
(define nullnode '())
(define leaf (lambda (x) (eqv? '() x) nullnode (list x nullnode nullnode nullnode)))
(define tritree (list 9 (leaf 1) (leaf 2) (leaf 3)))
; (display tritree) => (9 (1 () () ()) (2 () () ()) (3 () () ()))
(define lookup
(lambda (tr x)
(or (eqv? x (car tr))
(and (list? (cdr tr)) (lookup (cadr tr) x)))))
I want to get out of (leaf 1) and go through (leaf 2). How can I do that?
Is there a better way to define my lookup?
This is a straightforward implementation of how to search in a tree, it's just that this time we have three possible subtrees:
(define lookup
(lambda (tr x)
(cond ((null? tr) #f)
((equal? (first tr) x) #t) ; don't use eqv?, equal? is more general
(else
(or (lookup (second tr) x)
(lookup (third tr) x)
(lookup (fourth tr) x))))))
Alternatively, without using cond (and a bit closer to what you had in mind):
(define lookup
(lambda (tr x)
(and (not (null? tr))
(or (equal? (first tr) x)
(lookup (second tr) x)
(lookup (third tr) x)
(lookup (fourth tr) x)))))
And do notice that your implementation of leaf is incorrect, for the above to work you'll have to fix it first:
(define leaf
(lambda (x)
(if (null? x)
nullnode
(list x nullnode nullnode nullnode))))
It works as expected with the sample input:
(lookup tritree 3)
=> #t
(lookup tritree 5)
=> #f
I want to get all sublists that start with a number. So I did
(defun function (list)
(cond
((atom list) nil)
((and (numberq (car list)) (consp (car list)))
(cons (function (car list)) (number (cdr list))) )
((not (and (numberq (car list)) (consp (car list)))) (function (cdr list))) ) )
(function '((3 f g h) l s (v k) (2 m n) (9 d) c))
It returns nil instead of ((3 f g h) (2 m n) (9 d)).
Thank you for your help!
I guess this is roughly what you were trying to do:
(defun f (lst)
(when lst
(let ((e (car lst)))
(if (and (consp e) (numberp (car e)))
(cons e (f (cdr lst)))
(f (cdr lst))))))
Alternatively, you can use remove-if-not:
(defun f (lst)
(remove-if-not
(lambda (e) (and (consp e) (numberp (car e))))
lst))
In both cases, it works as expected:
? (f '((3 f g h) l s (v k) (2 m n) (9 d) c))
((3 F G H) (2 M N) (9 D))