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
Related
There are lots of manual how to make an input in scheme, but I do not understand how to work with it.
Lets say we have simple txt file as an input
a.txt:
1 2 3
I am calling standard io procedure on it
(let ((p (open-input-file "a.txt")))
(let f ((x (read p))) ; reading from file
(if (eof-object? x) ; check for eof
(begin
(close-input-port p)
'()
)
(cons x (f (read p))))))
output:
'(1 2 3)
Which is something I am looking for, but if I want to make standard pair/list manipulations on it I do not get what I expect:
(let ((p (open-input-file "a.txt")))
(let f ((x (read p))) ; reading from file
(if (eof-object? x) ; check for eof
(begin
(close-input-port p)
'()
)
(cdr
(cons x (f (read p)))))))
output:
'()
How do I get a hold of rest of the list?
Notice that the list you want to manipulate is constructed by the (let ...) expression, ie:
(let ((p ...)) ...) ;; => '(1 2 3)
So, to get the "rest" of the list, you have to apply cdr to the entire let expression, ie:
(cdr (let ((p ...)) ...)) ;; => '(2 3)
There is a difference to applying cdr outside the let expression as opposed to within. When you apply it outside, the let expression first gets evaluated to a list, which cdr then manipulates to get its "rest" values. This is equivalent to writing:
(cdr '(1 2 3)) ;; => '(2 3)
But if you apply it the way you have within its body, then at every recursive call for f, which is where a single cons cell is appended recursively to the list-so-far, you end up chaining a cdr to it, so instead of constructing a chain of cons as follows:
(cons 1 (cons 2 (cons 3 '()))) ;; => '(1 2 3)
you instead construct a chain of the form:
(cdr (cons 1 (cdr (cons 2 (cdr (cons 3 '())))))) ;; => '()
To see how this happens, it is a good idea to either think mentally about, or write down, the execution flow of your program. This enables you to understand what your expressions would evaluate to.
For example, your first let expression can be rewritten to an equivalent function as follows:
(define p (open-input-file "a.txt"))
;; (let f (...) ...) is a named let,
;; and is equivalent to the function below.
(define (f x)
(if (eof-object? x)
'()
(cons x (f (read p)))))
(f (read p))
(close-input-port p)
and its execution flow would look somewhat as follows:
(f (read p))
=> (f 1)
=> (cons 1 (f (read p)))
=> (cons 1 (f 2))
=> (cons 1 (cons 2 (f (read p))))
=> (cons 1 (cons 2 (f 3)))
=> (cons 1 (cons 2 (cons 3 (f (read p)))))
=> (cons 1 (cons 2 (cons 3 (f eof))))
=> (cons 1 (cons 2 (cons 3 '()))) ;; => '(1 2 3)
but if you apply cdr to your recursive call as follows:
(define (f x)
(if (eof-object? x)
'()
(cdr (cons x (f (read p))))))
then the execution would change to the following:
(f (read p))
=> (f 1)
=> (cdr (cons 1 (f (read p))))
=> (cdr (cons 1 (f 2)))
=> (cdr (cons 1 (cdr (cons 2 (f (read p))))))
=> (cdr (cons 1 (cdr (cons 2 (f 3)))))
=> (cdr (cons 1 (cdr (cons 2 (cdr (cons 3 (f (read p))))))))
=> (cdr (cons 1 (cdr (cons 2 (cdr (cons 3 (f eof)))))))
=> (cdr (cons 1 (cdr (cons 2 (cdr (cons 3 '()))))))
=> (cdr (cons 1 (cdr (cons 2 '()))))
=> (cdr (cons 1 '()))
=> '()
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 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))