This class will be recorded! Its use is limited to members of the class. Please do not share with others.
Approximate overview
Notes
Events
I’m not sure if all of these links are correct. Let me know if any are not.
map.Some of you need to work on your style. (I realize there’s a problem on it, but I saw things like
(equal? (list->string(take(string->list (substring contact (+ (char-index contact #\|) 1)))3)) "641")))
Which is way too much on one line, missing spaces, and more. You will lose points on LAs for poor style.
section to be the clearest,
at last with the three ways of subtracting five. We’ll see if that
continues.lambda for an anonymous procedure.
(map (lambda (x) (- x 3)) '(1 2 3 4 5))(map (section - <> 3) '(1 2 3 4 5))(map (o sub1 sub1 sub1) '(1 2 3 4 5))There’s no MP2 autograder. Why?
It appears that Sam is technologically incompetent, at least with regards to autograaders.
Maybe after class.
Why were you being mean about the code?
It was hard to read. I tend to be extreme on hard-to-read code.
Itwshrdtrd.Itndtbxtrmnhrd-t-rdcd.
Good formatting helps us read.
It was hard to read. I tend to be extreme on hard-to-read code.
Policies on formatting clearly?
Parameters to a procedure should all be on the same line or all in aligned vertically.
Try to get the first parameter on the same line as the procedure.
Try to keep lines under 80 characters.
Try to keep lines under 8 or so “words”/”units”
(define local?
(lambda (contact)
(equal? (list->string (take (string->list
(substring contact
(+ (char-index contact #\|) 1)))
3))
"641")))
What should conditionals look like?
(if TEST
CONSEQUENT
ALTERNATE)
(or EXP1
EXP2
...)
Can I map with and and or?
Not usually.
But if you were doing
(map odd? '(1 2 3 4 5))or something similar to get a list of true and false, you could instead useandmaporormap.
(andmap (lambda (x) x) '(#f #t #t #t))
(ormap (lambda (x) x) '(#f #t #t #t))
> (andmap (lambda (x) x) '(#f #t #t #t))
#f
> (andmap (lambda (x) x) '(#t #t #t #t))
#t
> (ormap (lambda (x) x) '(#t #t #t #t))
#t
> (ormap (lambda (x) x) '(#f))
#f
We need to have a function for
andmapandormap. We use(lambda (x) x)which is “a function that takes one parameter and returns that parameters”.
(define id (lambda (x) x))
(andmap id '(#t #f #t #f))
Could you please be nicer?
Maybe
Can I replace a character in a string?
(string-replace "abcde" "c" "Q")
Can I replace selected elements in a list?
> (define bluecow
(lambda (x)
(if (equal? x "cow")
"blue"
x)))
> (map bluecow (list "cat" "and" "cow" "jump"))
'("cat" "and" "blue" "jump")
Can I replace the value of a list at index 5?
Lists are immutable. You cannot change them. You can only build new lists.
> (define cowblue
(lambda (lst)
(append (take lst 5) (list "blue") (drop lst 6))))
> (cowblue (make-list 10 "cow"))
'("cow" "cow" "cow" "cow" "cow" "blue" "cow" "cow" "cow" "cow")
> (cowblue (range 1 10))
'(1 2 3 4 5 "blue" 7 8 9)