Lisp Scheme -cannot reference undefined identifier error - scheme

I am new to Lisp Scheme .
I am trying to write Caesar cipher program using the Lisp Scheme on DrRacket IDE.
I am getting the following
paragraph: undefined;
cannot reference undefined identifier
My code is as follow
;; ENVIRONMENT
;; contains "ctv", "vtc",and "reduce" definitions
(load "include.ss")
;; contains a test document consisting of three paragraphs.
(load "document.ss")
;; contains a test-dictionary, which has a much smaller dictionary for testing
;; the dictionary is needed for spell checking
(load "test-dictionary.ss")
(load "dictionary.ss") ;; the real thing with 45,000 words
;; -----------------------------------------------------
;; HELPER FUNCTIONS
;; returns a function to "spin" arg "letter" by "val"
(define spin
(lambda (val)
(lambda (letter)
(vtl(modulo (+ val (ltv letter)) 26)))))
;; returns a function to spin current letter to value of 'e'
(define spin_to_e
(lambda (pos)
(+ (- 26 pos) 4)
))
;;counts how many of a target letter are in a word. (count_letter 'x) returns a func that counts x in a list. ((count_letter 'x) '(list)) counts x in list
(define count_letter
(lambda (ltr)
(lambda (lis)
(if (null? lis)
0
(if (equal? (car lis) ltr)
(+ 1 ((count_letter ltr) (cdr lis)))
(+ 0 ((count_letter ltr) (cdr lis)))))
)))
;;counts how many elements in a list == #t
(define count_true
(lambda (l)
(reduce + (map (lambda (x) (if (equal? x #t) 1 0)) l) 0) )) ;;HERE'S MY USE OF REDUCE. Count's how many #t's are in a list
;;show me the truth
(define containstrue?
(lambda (l)
(if (null? l)
#f
(if (equal? (car l) #t)
#t
(containstrue? (cdr l))))
))
(define (sortappend l piece) ;;adds one by one, putting element at the head if >= current head
(if (null? l) (append '() l)
(if (>= piece (car l))
(cons piece l)
(append l (list piece)))))
(define find_index ;;finds target element, returns its "index" from 0 26
(lambda (l t)
(if (null? l) 0
(if (equal? t (car l))
0
(+ 1 (find_index (cdr l) t)) ))
))
;; *** CODE FOR ANY HELPER FUNCTION GOES HERE ***
;; -----------------------------------------------------
;; SPELL CHECKER FUNCTION
;;check a word's spell correctness
;;INPUT:a word(a global variable "dictionary" is included in the file "test-dictionary.ss", and can be used directly here)
;;OUTPUT:true(#t) or false(#f)
(define spell-checker
(lambda (w)
(if (member w dictionary) #t #f)
;; DONE
))
;; -----------------------------------------------------
;; ENCODING FUNCTIONS
;;(define (multiplyBy n) (lambda (x) (* n x)))
;;((multiplyBy 5) 2) ^^how to ret a function
;;generate an Caesar Cipher single word encoders
;;INPUT:a number "n"
;;OUTPUT:a function, whose input=a word, output=encoded word
(define encode-n
(lambda (n);;"n" is the distance, eg. n=3: a->d,b->e,...z->c
(lambda (w);;"w" is the word to be encoded
(map (spin n) w) ;;performs helper func 'spin' on every letter in given word w
)))
;;encode a document
;;INPUT: a document "d" and a "encoder"
;;OUTPUT: an encoded document using a provided encoder
(define encode-d;;this encoder is supposed to be the output of "encode-n"
(lambda (d encoder)
(if (null? d) '() ;;catches recursive base case, returns empty list
(append (cons(map encoder (car d)) '()) (encode-d (cdr d) encoder) )) ;;encode first para, concat w/ recursive call on rest
))
;; -----------------------------------------------------
;; DECODE FUNCTION GENERATORS
;; 2 generators should be implemented, and each of them returns a decoder
;;generate a decoder using brute-force-version spell-checker
;;INPUT:an encoded paragraph "p"
;;OUTPUT:a decoder, whose input=a word, output=decoded word
(define Gen-Decoder-A
(lambda (p)
(define return_decoder
(lambda (i)
(encode-n i)
))
(define valid_wordcounts
(map count_true
(list
(map spell-checker (map (encode-n 0) p))
(map spell-checker (map (encode-n 1) p))
(map spell-checker (map (encode-n 2) p))
(map spell-checker (map (encode-n 3) p))
(map spell-checker (map (encode-n 4) p))
(map spell-checker (map (encode-n 5) p))
(map spell-checker (map (encode-n 6) p))
(map spell-checker (map (encode-n 7) p))
(map spell-checker (map (encode-n 8) p))
(map spell-checker (map (encode-n 9) p))
(map spell-checker (map (encode-n 10) p))
(map spell-checker (map (encode-n 11) p))
(map spell-checker (map (encode-n 12) p))
(map spell-checker (map (encode-n 13) p))
(map spell-checker (map (encode-n 14) p))
(map spell-checker (map (encode-n 15) p))
(map spell-checker (map (encode-n 16) p))
(map spell-checker (map (encode-n 17) p))
(map spell-checker (map (encode-n 18) p))
(map spell-checker (map (encode-n 19) p))
(map spell-checker (map (encode-n 20) p))
(map spell-checker (map (encode-n 21) p))
(map spell-checker (map (encode-n 22) p))
(map spell-checker (map (encode-n 23) p))
(map spell-checker (map (encode-n 24) p))
(map spell-checker (map (encode-n 25) p)))))
(return_decoder
(find_index valid_wordcounts (apply max valid_wordcounts)))
))
;;generate a decoder using frequency analysis
;;INPUT:same as above ;;sample of a doc, so a para
;;OUTPUT:same as above
(define Gen-Decoder-B
(lambda (p)
(define return_decoder
(lambda (i)
(encode-n i)
))
(define lettercounts
(list
(reduce + (map (count_letter 'a)(reduce append (list p) '())) 0)
(reduce + (map (count_letter 'b)(reduce append (list p) '())) 0)
(reduce + (map (count_letter 'c)(reduce append (list p) '())) 0)
(reduce + (map (count_letter 'd)(reduce append (list p) '())) 0)
(reduce + (map (count_letter 'e)(reduce append (list p) '())) 0)
(reduce + (map (count_letter 'f)(reduce append (list p) '())) 0)
(reduce + (map (count_letter 'g)(reduce append (list p) '())) 0)
(reduce + (map (count_letter 'h)(reduce append (list p) '())) 0)
(reduce + (map (count_letter 'i)(reduce append (list p) '())) 0)
(reduce + (map (count_letter 'j)(reduce append (list p) '())) 0)
(reduce + (map (count_letter 'k)(reduce append (list p) '())) 0)
(reduce + (map (count_letter 'l)(reduce append (list p) '())) 0)
(reduce + (map (count_letter 'm)(reduce append (list p) '())) 0)
(reduce + (map (count_letter 'n)(reduce append (list p) '())) 0)
(reduce + (map (count_letter 'o)(reduce append (list p) '())) 0)
(reduce + (map (count_letter 'p)(reduce append (list p) '())) 0)
(reduce + (map (count_letter 'q)(reduce append (list p) '())) 0)
(reduce + (map (count_letter 'r)(reduce append (list p) '())) 0)
(reduce + (map (count_letter 's)(reduce append (list p) '())) 0)
(reduce + (map (count_letter 't)(reduce append (list p) '())) 0)
(reduce + (map (count_letter 'u)(reduce append (list p) '())) 0)
(reduce + (map (count_letter 'v)(reduce append (list p) '())) 0)
(reduce + (map (count_letter 'w)(reduce append (list p) '())) 0)
(reduce + (map (count_letter 'x)(reduce append (list p) '())) 0)
(reduce + (map (count_letter 'y)(reduce append (list p) '())) 0)
(reduce + (map (count_letter 'z)(reduce append (list p) '())) 0)))
(return_decoder (spin_to_e (find_index lettercounts (apply max lettercounts))))
))
;; -----------------------------------------------------
;; CODE-BREAKER FUNCTION
;;a codebreaker
;;INPUT: an encoded document(of course by a Caesar's Cipher), a decoder(generated by functions above)
;;OUTPUT: a decoded document
(define Code-Breaker
(lambda (d decoder)
(if (null? d) '() ;;catches recursive base case, returns empty list
(append (cons(map decoder (car d)) '()) (Code-Breaker (cdr d) decoder)) ) ;;encode first para, concat w/ recursive call on rest
))
;; -----------------------------------------------------
;; EXAMPLE APPLICATIONS OF FUNCTIONS
(spell-checker '(h e l l o))
(define add5 (encode-n 5))
(encode-d document add5)
;(define decoderSP1 (Gen-Decoder-A paragraph))
(define decoderFA1 (Gen-Decoder-B paragraph))
(Code-Breaker document decoderSP1)
The IDE shows the error at the second last line of the code
(define decoderFA1 (Gen-Decoder-B paragraph)).
The error is due to paragraph .
Help me fix this error. Thanks in advance for helping
I have the necessary external files , but those only contain the list

You are using DrRacket to write a Scheme program. Since DrRacket can be used with many languages, you need to choose R5RS Scheme as your language:
1. In the lower left corner of DrRacket, click the drop down menu.
2. Click "Choose Language..."
3. Choose R5RS Scheme.
Also remember to copy the files "include.ss", "document.ss", "test-dictionary.ss" and "dictionary.ss" into the same directory as your own source file.
PS: Is it this project?
https://github.com/T-G-P/CaeserDawg/blob/master/proj2.pdf

Related

Luhn algorithm in scheme

When I have a number in my list that is greater than 9 I want to separate the
digits and add them to the running sum.
The code I have is giving me and error in my sum-list definition.
(define sum-list (lst)
(if (null lst)
0
(if (>9 car lst?)
(cons ((mod (car lst) 10)) + (* (remainder (/car lst 10) 10))))
(if (>9 cdr lst?)
(cons ((mod (cdr lst)10)) + (* (remainder (/cdr lst 10) 10))))
(+ (car lst) (sum-list (cdr lst)))))
I am getting an error"Expected only one expression after the name sum-list but found one extra part.
I wrote this now in mit-scheme. I split the problem in 2 subproblems -- to conver the number to the list of digits and then to sum the digits in the resulting list.
(define n->l
(lambda (n return)
((lambda (s) (s s n return))
(lambda (s n col)
(if (zero? n)
(col '())
(s s
(quotient n 10)
(lambda (rest)
(col (cons (remainder n 10) rest)))))))))
(define sum-digits
(lambda (n)
(n->l n (lambda (l) (fold-left + 0 l)))))
(sum-digits 100)
(sum-digits 123)

Producing a list of lists

I am trying to produce a list of lists which has *.
Here is what I have so far:
(define (position loc count)
(cond [(empty? loc)empty]
[else (cons (list (first loc) count)
(position (rest loc) (add1 count)))]
))
So:
(position (string->list "**.*.***..") 0)
would produce:
(list
(list #\* 0) (list #\* 1) (list #\. 2) (list #\* 3) (list #\. 4) (list #\* 5)
(list #\* 6) (list #\* 7) (list #\. 8) (list #\. 9))
Basically I am trying to get
(list (list (list #\* 0) (list #\* 1))
(list (list #\* 3))
(list (list #\* 5)(list #\* 6) (list #\* 7)))
I thought about using foldr but not sure if that will work. Any help would be appreciated.
It's not exactly a foldr solution though, you need a function that modifies it's behaviour based on prior input in order to group the continuous star characters. Check out my use of a boolean to switch behaviour upon finding a match.
(define (combine-continuous char L)
(let loop ((L L) (acc '()) (continuing? #t))
(cond ((null? L) (list (reverse acc)))
((equal? (caar L) char)
(if continuing?
(loop (cdr L) (cons (car L) acc) #t)
(cons (reverse acc)
(loop (cdr L) (list (car L)) #t))))
(else (loop (cdr L) acc #f)))))
(combine-continuous #\* (position (string->list "**.*.***..") 0))
=->
;Value 19: (((#\* 0) (#\* 1)) ((#\* 3)) ((#\* 5) (#\* 6) (#\* 7)))

What is definition of “map” in Racket

What would be the definition of "map" function without using any other high-level functional in Racket?
I need a stack recursion version.
A simple definition of a map function could be:
(define (map f l)
(if (null? l)
'()
(cons (f (car l)) (map f (cdr l)))))
(map (lambda (n) (* n n)) '(1 2 3 4)) ;; => (1 4 9 16)
Usually you'll find map being made with fold, but I prefer doing everything with pair-for-each (maplist in CL). This defines pair-for-each, map, filter-map, filter, zip and unzip compatible with the same procedures in SRFI-1 List library.
#!racket/base
(define-values (pair-for-each map filter-map filter zip unzip)
(let ((%MAP-PASS (list 'MAP-PASS))
(%MAP-END (list 'MAP-END)))
;; pair-for-each-1 applies proc to every cons
;; in order until proc returns %MAP-END
;; when proc evaluates to %MAP-PASS the result is skipped
(define (pair-for-each-1 proc lst (next cdr))
(let loop ((lst lst))
(let ((res (proc lst)))
(cond ((eq? res %MAP-END) '())
((eq? res %MAP-PASS) (loop (next lst)))
(else (cons res
(loop (next lst))))))))
;; Transform a typical map procedure to include
;; a %MAP-END when the list argument is eq? a certain value
(define (stop-at value proc)
(lambda (lst)
(if (eq? value lst)
%MAP-END
(proc lst))))
;; Takes a lists of lists and returns a
;; new list with the cdrs
(define (cdrs lsts)
(pair-for-each-1 (stop-at '() cdar) lsts))
;; Takes a list of lists and returns a
;; new list with the cars except if one of
;; the sublists are nil in which the result is also nil
(define (cars lsts)
(call/cc (lambda (exit)
(pair-for-each-1 (stop-at '()
(lambda (x)
(let ((x (car x)))
(if (null? x)
(exit '())
(car x)))))
lsts))))
;; Takes a list of lists and returns #t if any are null
(define (any-null? lsts)
(if (null? lsts)
#f
(or (null? (car lsts))
(any-null? (cdr lsts)))))
;; Return value definitions starts here
;; pair-for-each is called maplist in CL
(define (pair-for-each proc lst . lsts)
(if (null? lsts)
(pair-for-each-1 (stop-at '() (lambda (x) (proc x))) lst)
(pair-for-each-1 (lambda (args)
(if (any-null? args)
%MAP-END
(apply proc args)))
(cons lst lsts)
cdrs)))
;; Multi arity map
(define (map f lst . lsts)
(if (null? lsts)
(pair-for-each-1 (stop-at '() (lambda (x) (f (car x)))) lst)
(pair-for-each-1 (lambda (x)
(let ((args (cars x)))
(if (null? args)
%MAP-END
(apply f args))))
(cons lst lsts)
cdrs)))
;; filter-map is like map except it skips false values
(define (filter-map proc . lsts)
(apply map (lambda x
(or (apply proc x) %MAP-PASS)))
lsts)
;; filter only takes one list and instead of the result it
;; takes the original argument as value (which may be #f)
(define (filter predicate? lst)
(pair-for-each-1 (stop-at '()
(lambda (x)
(let ((x (car x)))
(if (predicate? x)
x
%MAP-PASS))))
lst))
;; zip (zip '(1 2 3) '(a b c)) ; ==> ((1 a) (2 b) (3 c))
(define (zip lst . lsts)
(apply map list (cons lst lsts)))
;; unzip does the same except it takes a list of lists as argument
(define (unzip lsts)
(apply map list lsts))
;; return procedures
(values pair-for-each map filter-map filter zip unzip)))
It was unclear to me what kind of implementation the OP asked for, so here is yet another variation of map.
; map : function list -> list
; (map f '()) = '()
; (map f (cons x xs)) = (cons (f x) (map f xs))
(define (my-map f xs)
; loop : list list -> list
; (loop (list x1 ... xn) (list y1 ... ym)) = (list (f x1) ... (f xn) ym ... y1)
(define (loop xs ys)
(match xs
['() (reverse ys)]
[(cons x xs) (loop xs (cons (f x) ys))]))
(loop xs '()))
Example:
(my-map sqrt '(1 4 9 16))
'(1 2 3 4)

Encapsulating Certain Parts of List

I'm trying to write a procedure that "encapsulates" (i.e. puts in a list) elements of a list between a "separator" element.
(my-proc '(1 + 2))
=> ((1) (2))
(my-proc '(x * y + z ^ 2 + 1 + 5))
=> ((x * y) (z ^ 2) (1) (5))
(my-proc '((x + 1) * y + 5))
=> (((x + 1) * y) (5))
In this case the procedure can be hard-coded to define the + symbol as the separator.
Assume that foldr (fold right operation) is defined, I'd prefer that it'd be in terms of it.
I'm not giving a full solution since this looks really homework-y.
(define (split-expr expr)
(foldr (lambda (e es)
(if (eq? e '+)
<???> ; do split
(cons (cons e (car es))
(cdr es))))
<???> ; what should start be?
es))
Just for fun, here's a version in continuation-passing style (no foldr, probably not suitable as a homework answer):
(define split/cps
(λ (sep ls)
(let loop ([ls ls] [k (λ (item acc)
(if item (cons item acc) acc))])
(cond
[(null? ls)
(k #f '())]
[(eq? sep (car ls))
(loop (cdr ls)
(λ (item acc)
(k #f (if item (cons item acc) acc))))]
[else
(loop (cdr ls)
(λ (item acc)
(k (if item
(cons (car ls) item)
(list (car ls)))
acc)))]))))
Here's another way to do it, also without foldr:
(define split/values
(λ (sep ls)
(let loop ([ls ls])
(cond
[(null? ls)
'()]
[else
(let-values ([(a d) (car-to-sep sep ls)])
(if (null? a)
(loop d)
(cons a (loop d))))]))))
(define car-to-sep
(λ (sep ls)
(let loop ([ls ls] [a '()])
(cond
[(null? ls)
(values '() '())]
[(eq? sep (car ls))
(values '() (cdr ls))]
[else
(let-values ([(a d) (loop (cdr ls) a)])
(values (cons (car ls) a) d))]))))

Matrix addition in Scheme

I am trying to add a matrix and it is not working...
(define (matrix-matrix-add a b)
(map (lambda (row) (row-matrix-add row b))
a))
(define (row-matrix-add row matrix)
(if (null? (car matrix))
'()
(cons (add-m row (map car matrix))
(row-matrix-add row (map cdr matrix)))))
(define (add-m row col)
(if (null? col)
0
(+ (car row)
(car col)
(add-m (cdr row) (cdr col)))))
Here is very short working implementation. Map is good at getting rid of a layer of recursion, when you can use it.
(define (matrix-add x y) (map (lambda (x y) (map + x y)) x y))
Here is a working implementation:
(define (matrix-add m1 m2)
(define (matrix-add-row r1 r2 res-row)
(if (and (not (null? r1)) (not (null? r2)))
(matrix-add-row (cdr r1) (cdr r2)
(cons (+ (car r1) (car r2)) res-row))
(reverse res-row)))
(define (matrix-add-each m1 m2 res)
(if (and (not (null? m1)) (not (null? m2)))
(let ((res-row (matrix-add-row (car m1) (car m2) ())))
(matrix-add-each (cdr m1) (cdr m2) (cons res-row res)))
(reverse res)))
(matrix-add-each m1 m2 ()))
Sample usage and output:
> (matrix-add '((7 2) (3 8)) '((4 8) (0 5)))
((11 10) (3 13))
> (matrix-add '((5 2) (4 9) (10 -3)) '((-11 0) (7 1) (-6 -8)))
((-6 2) (11 10) (4 -11))

Resources