Syntax error when using parentheses instead of brackets - racket

I thought that brackets and parentheses were interchangeable in Racket.
However, when I run the following code, I get the syntax error, else: not allowed as an expression in: (else (cons (car lat) (rember a (cdr lat))))
#lang racket
(define rember
(lambda (a lat)
(cond
((null? lat) (quote ()))
((eq? a (car lat) (cdr lat))
(else
(cons (car lat) (rember a (cdr lat))))))))
The error goes away when I replace the parentheses in cond with brackets:
#lang racket
(define rember
(lambda (a lat)
(cond
[(null? lat) (quote ())]
[(eq? a (car lat)) (cdr lat)]
[else
(cons (car lat) (rember a (cdr lat)))])))
So it appears that there is a syntactic difference between parentheses and brackets. Is this the case or am I missing something?

You forgot one parenthesis in ((eq? a (car lat) (cdr lat)). If you rewrite that correctly, both versions will work:
(define rember
(lambda (a lat)
(cond
((null? lat) (quote ()))
((eq? a (car lat)) (cdr lat))
(else
(cons (car lat) (rember a (cdr lat)))))))

Related

little schemer drracket error cannot reference an identifier before its definition

beginner question,
just started the little schemer book and installed DrRacket on my macbook to try some of the code examples.
If I choose Racket language, the following code
#lang Racket
(define (atom? x)
(and (not (pair? x)) (not (null? x))))
(atom? '())
(define lat?
(lambda (l)
(cond
((null? l) #t)
((atom? (car l)) (lat? (cdr l)) )
(else #f))))
(lat? (a b))
will trigger error message:
a: unbound identifier in module in: a
if I choose R5RS language,
#lang R5RS
(define (atom? x)
(and (not (pair? x)) (not (null? x))))
(atom? '())
(define lat?
(lambda (l)
(cond
((null? l) #t)
((atom? (car l)) (lat? (cdr l)) )
(else #f))))
(lat? (a b))
I got an error message:
#%plain-module-begin: illegal use (not a module body) in: (#%plain-module-begin (module configure-runtime racket/base (require r5rs/init)) (define (atom? x) (and (not (pair? x)) (not (null? x)))) (atom? (quote ())) (define lat? (lambda (l) (cond ((null? l) #t) ((atom? (car l)) (lat? (cdr l))) (else #f)))) (lat? (a b)))
Anyone know what I did wrong?
Thanks
Looks like that last call should be
(lat? '(a b))
... no?
(Also: I would recommend using #lang racket in general, but I strongly suspect that your problem R5RS arises because you're "setting the language twice"; if you start your program with #lang R5RS, you don't need to change the language level. Conversely, if you set the language level, you shouldn't start your program with #lang R5RS. If you do both, I'm guessing you get the error message you saw.)

Racket Nested-Lists and applying functions to them

I'm trying to teach myself Racket. I'm currently trying to write a function to help understand nested lists. The function takes a nested list and a procedure and applies the procedure to each element to produce a new list. An example:
(map-tree even? '(1 2 3 4)) => '(#f #t #f #t)
Here's what I've got so far:
(define (map-tree proc tree)
(map-tree-aux tree proc '() ))
(define (map-tree-aux tree proc lst)
(if (null? tree)
lst
(if (list? tree)
(if (null? (cdr tree))
(if (number? (car tree))
(map-tree-aux (car tree) proc (append-end (proc (car tree)) lst))
(map-tree-aux (car tree) proc lst))
(if (number? (car tree))
(map-tree-aux (cdr tree) proc (append-end (proc (car tree)) (map-tree-aux (car tree) proc lst)))
(map-tree-aux (cdr tree) proc lst)))
lst)))
(define (append-end elem lst)
(append lst (list elem)))
While this works with the original example I supplied, a more complex example comes out incorrectly:
(map-tree even? '(1 (2 (3 (4))))) should be '(#f (#t (#f (#t)))), but is currently (#f #t #f #t).
I know it's just a matter is "listing" somewhere, but I'm having an issue finding out how to do it.
My first thought was to apply the list procedure to the lst if the tree is null and (car tree) is not a number, but I get the opposite of what I want (the resultant list is nested in the opposite direction). I'd really appreciate your help.
Thanks!
When iterating over list of lists, the general idea for the cases to check is:
if list is empty (null? lst), do something ...
if the first item in list is atomic (not (pair? (car lst))), do something else ...
if the first item in list is a list itself (pair? (car lst)), else ...
Choosing the right construct is also important, ie. instead of nesting if statements, using cond or match etc. is preferred.
Also try and avoid using non-constant time procedures (such as append) in your recursive steps to improve efficiency.
With these in mind, one approach to create the function in question is by simply using cons to build a new list while preserving the structure of the old, as follows:
(define (my-map pred lst)
(cond
((null? lst) '())
((not (pair? (car lst)))
(cons (pred (car lst))
(my-map pred (cdr lst))))
(else
(cons (my-map pred (car lst))
(my-map pred (cdr lst))))))
You can write the same function using match instead of cond:
(define (my-map pred lst)
(match lst
['() '()]
[(cons (? pair?) b)
(cons (my-map pred (car lst))
(my-map pred (cdr lst)))]
[(cons a b)
(cons (pred (car lst))
(my-map pred (cdr lst)))]))
You can also build a tail-recursive function that does this:
(define (my-map pred lst)
(let loop ((lst lst)
(acc '()))
(cond
((null? lst)
(reverse acc))
((not (pair? (car lst)))
(loop (cdr lst) (cons (pred (car lst)) acc)))
(else
(loop (cdr lst) (cons (loop (car lst) '()) acc))))))
Notice that (reverse acc) is returned in the base case because the list being built in the accumulator acc is in reverse order from the original list lst. To avoid this, we can modify this function to accumulate a continuation instead:
(define (my-map pred lst)
(let loop ((lst lst)
(acc identity))
(cond
((null? lst)
(acc '()))
((not (pair? (car lst)))
(loop (cdr lst) (lambda (r)
(acc (cons (pred (car lst)) r)))))
(else
(loop (cdr lst)
(lambda (r)
(acc (cons (loop (car lst) identity) r))))))))
For all cases, you will have:
(my-map even? '(1 2 3 4 5 7))
=> '(#f #t #f #t #f #f)
(my-map even? '(1 (2 (3 (4 (5 (7)))))))
=> '(#f (#t (#f (#t (#f (#f))))))

Segmentation fault when trying to reverse a list

I'm learning Common Lisp and I've been trying to write a function to reverse a list. This is the function but every time I try to run it I get "Segmentation Fault (Core Dumped)"
(defun reverseList(x)
(if (cdr x)
(cons (reverseList x) (car x))
(car x)))
(reverseList '(1 2 3))
I think this will work:
(define reverseList(lambda(x)(
if (null? (cdr x))
(cons (car x) '())
(append (reverseList (cdr x)) (cons (car x) '()))
)
)
)

Lisp prefix calculator

How can I write a calculator to prefix notation, when it should count this example '(+ * 3 2 - 2 1), where there are no brackets between characters? When I have the brackets, I can handle it, but in this case I am lost.
A quick search on google yielded this wikipedia page, with an implementation (in pseudocode) of prefix evaluation using a stack. It's a starting point for writing your own implementation. Also notice that a stack can be easily implemented using Lisp's linked lists, simply add/remove elements at the head.
I'm a beginner in scheme (excuse me if it is nasty code). This one is with the brackets:
(define (fce a)
(case a
(( + ) +)
(( - ) -)
(( * ) *)
(( / ) /)))
(define (analyze vyraz)
(if (list? vyraz)
(calcul vyraz)
vyraz))
(define (calcul vyraz)
(if (= (length vyraz) 3 )
((fce (car vyraz)) (analyze (cadr vyraz)) (analyze (caddr vyraz)))
(calcul (cons (calcul (list (car vyraz) (cadr vyraz) (caddr vyraz) (cadddr vyraz)))))))
this code can count for example this : (calcul '(- (* 3 2)(+ 1 2))
I wanted to repair this code to code without brackets but conditions and listing the original input stopped me. I do not know how to write the conditions so that I didn't lose the original input and also came the final calculation.
there are my conditions (but i think, there are at wrong syntax
(define (count vyraz)
(cond (list? vyraz)
(number? (car vyraz) (cons (car vyraz)))
(number? (cdr vyraz) (cons (cdr vyraz)))
(symbol? (cddr vyraz) (cons (cddr vyraz)))
(else (cdr vyraz) (count vyraz)))
(calcul vyraz))
Thank you for all your answers and tips :)
(define (polish-notation-eval expr)
(if (finished? expr)
(car expr)
(polish-notation-eval (the-once-over expr))))
;;either we are finished, or we need to make the-once-over to get closer to finished.
(define (the-once-over expr)
(cond ((null? expr) expr )
((well-formed-expr? expr)
(cons ((get-operator (car expr)) (cadr expr) (caddr expr))
(the-once-over (cdddr expr))))
(else (cons (car expr) (the-once-over (cdr expr))))))
;; just scrolls down the list looking for well-formed expressions to evaluate.
(define *the-operations*
(list (list '+ +)
(list '* *)
(list '- -)
(list '/ /)))
;; an association list with names and functions
(define (get-operator name)
(cadr (assoc name *the-operations*)))
;;returns the function that goes with the name
(define (finished? expr)
(= (length expr) 1))
;; we are finished one we get down to a length one list, otherwise not
(define (well-formed-expr? expr)
(and (assoc (car expr) *the-operations*)
(number? (cadr expr))
(number? (caddr expr))))
;;are the next three elements something we can go ahead and evaluate?

Use lisp to output something while in cond

(define length1
(lambda (lat)
(cond
((null? lat) 0)
(else (+ 1 (length1 (cdr lat)))))))
for example: display the number (or anything else) when call length1 in cond
for common lisp you can use (progn (...) (...) ...) to group together multiple expressions into one.
the equivalent in scheme is (begin (...) (...) ...).
so:
(define length1
(lambda (lat)
(cond
((null? lat) 0)
(else (begin (display "hello world") (+ 1 (length1 (cdr lat))))))))
or maybe you want:
(define length1
(lambda (lat)
(cond
((null? lat) 0)
(else (let ((or-anything-else (+ 1 (length1 (cdr lat)))))
(display or-anything-else)
or-anything-else)))
and that's about exhausted my patience.