Concrete Abstractions: Chapter 9

Exercise 9.2: The sequences we just described are restricted to consecutive increasing sequences of integers (more precisely, to increasing arithmetic sequences where consecutive elements differ by 1). We can easily imagine similar but more general sequences such as <6, 4, 3, 2> or <5, 5.1, 5.2, 5.3, 5.4, 5.5>—in other words, general arithmetic sequences of a given length, starting value, and increment (with decreasing sequences having a negative increment value).
a. Write a procedure sequence-with-from-by that takes as arguments a length, a starting value, and an increment and returns the corresponding arithmetic sequence. Thus, (sequence-with-from-by 5 6 -1) would return the first and (sequence-with-from-by 6 5 .1) would return the second of the two pre- ceding sequences. Remember that sequences are represented as procedures, so your new sequence constructor will need to produce a procedural result.
b. The procedure sequence-from-to can now be rewritten as a simple call to sequence-with-from-by. The original sequence-from-to procedure made an empty sequence if its first argument was greater than its second, but you should make the new version so that you can get both increasing and de- creasing sequences of consecutive integers. Thus, (sequence-from-to 3 8) should return <3, 4, 5, 6, 7, 8>, whereas (sequence-from-to 5 1) should return <5, 4, 3, 2, 1>.
c. Write a procedure sequence-from-to-with that takes a starting value, an ending value, and a length and returns the corresponding arithmetic sequence. For example, (sequence-from-to-with 5 11 4) should return <5, 7, 9, 11>.

Solution:

; a.
(define sequence-with-from-by
  (lambda (start len inc)
    (lambda (op)
      (cond ((equal? op 'empty-sequence?)
              (= len 0))
            ((equal? op 'head)
               start)
            ((equal? op 'tail)
               (sequence-with-from-by (+ start inc) (- len 1) inc))
            ((equal? op 'sequence-length)
               len)
            (else
               (error "illegal operation" op))))))

; b.
(define sequence-from-to
  (lambda (a b)
    (if (< a b)
      (sequence-with-from-by a (+ (- b a) 1) 1)
      (sequence-with-from-by a (+ (- a b) 1) (- 1)))))

; c.
(define sequence-from-to-with
  (lambda (start end len)
   (sequence-with-from-by start len (/ (+ (- end start) 2) len))))

Exercise 9.6: Write the sequence constructor sequence-map, that outwardly acts like the list procedure map. However unlike map, which applies the procedural argument to all the list elements, sequence-map should not apply the procedural argument at all yet. Instead, when an element of the resulting sequence (such as its head) is accessed, that is when the procedural argument should be applied.

Solution:

(define sequence-map
  (lambda (sequence f)
   (lambda (op)
     (cond
       ((equal? op 'empty-sequence?)
         (empty-sequence? sequence))
       ((equal? op 'head)
         (f (head sequence)))
       ((equal? op 'tail)
         (sequence-map (tail sequence) f))
       ((equal? op 'sequence-length)
         new-length)
       ((equal? op 'sequence-ref)
        (lambda (n)
         (if (= n 0)
           head
           (sequence-ref tail (- n 1)))))
       (else (error "illegal sequence operation" op))))))

Exercise 9.: One way we can represent a set is as a predicate (i.e., a procedure that returns true or false). The idea is that to test whether a particular item is in the set, we pass it to the procedure, which provides the answer. For example, using this representation, the built-in procedure number? could be used to represent the (infinite) set of all numbers.
a. Implement element-of-set? for this representation. It takes two arguments, an element and a set, and returns true or false depending on whether the element is in the set or not.
b. Implement add-to-set for this representation. It takes two arguments, an ele- ment and a set, and returns a new set that contains the specified element as well as everything the specified set contained. Hint: Remember that a set is represented as a procedure.
c. Implement union-set for this representation. It takes two arguments—two sets— and returns a new set that contains anything that either of the provided sets contains.

Solution:

; a
(define element-of-set?
  (lambda (element set)
   (set element)))

; b.
(define add-to-set
  (lambda (element set)
    (lambda (ele)
      (if (equal? ele element)
        #t
        (set ele)))))

; c.
(define union-set
  (lambda (set1 set2)
    (lambda (ele)
      (or (set1 ele) (set2 ele)))))

Exercise 9.: Assume that infinity has been defined as a special number that is greater than all normal (finite) numbers and that when added to any finite number or to itself, it yields itself. Now there is no reason why sequences need to be of finite length. Write a constructor for some interesting kind of infinite sequence.

Solution:

; Ex 9.22
(define sequence-from-to-inf
  (lambda (from)
    (lambda (op)
     (cond ((equal? op 'head)
        from)
           ((equal? op 'tail)
        (sequence-from-to-inf (+ 1 from)))
           ((equal? op 'sequence-length)
         +inf.0)
           (else
         (error "operation not found" op))))))</code>

; Interesting application
(define display-even-sequence
  (lambda (seq)
    (if (even? (head seq))
        (display (head seq))
        (display " "))
        (display-even-sequence (tail seq))))

(define display-f-sequence
  (lambda (seq f)
    (display (f (head seq)))
    (display " ")
    (display-f-sequence (tail seq) f)))

#61/111: Search Engine Optimization: An Hour a Day

What is it about?

You heard of Search Engine Optimization (SEO) but don’t really know what is about? No problem, Jennifer Grappone and Gradiva Couzin will help you to start your SEO campaign and execute it successfully.

What can I learn?

Choose and use your keywords: If you want to be found when someone is searching for water bottles, you have to write about water bottles. This is a fundamental step. A good tip for selecting keywords is using composed keywords, e.g. plastic water bottles or sports water bottles. Over the time search queries increased in length. The average query is now about four words long.

Create Buzz: A major attribute of good search ranking are backlinks. Create content people want to link to. Create Buzz. Write things that people want to share. How-to lists, Top X lists, videos and pictures/infographics.

Measure your success: You can’t control what you don’t measure. Start using a simple analytics software like Google Analytics. This allows you to see how many people visited your site, which sites and where did they come from. However, don’t be discouraged if your traffic isn’t roaring immediately. Everything takes time.

Conclusion

Search Engine Optimization: An Hour a Day is a very basic text which is appropriate if you never read about or done some SEO. The authors present the key principles neatly structured and allow you to follow a plan to increase your search engine ranking. In conclusion, if you’ve never done anything with SEO or don’t have much time, this is a book for you.

#56/111: Permission Marketing

What is it about?

How do you advertise? Seth Godin differentiates between interruption marketing (e.g. banner ads, TV ads, magazine ads) and permission marketing (sending information directly to people who accepted to send your information). 

What can I learn?

Leverage interruption marketing: Permission marketing is often a bit mis-defined. It’s not about stopping your advertising, it’s about using it better. You have basically two options in advertising: a) You try to make a sale directly or b) You try to get the permission to give them more information. Seth recommends b) because it’s a less expensive step for your prospect (giving away their email vs. giving away twenty bucks) and you have a less expensive channel for frequent information (sending emails vs. buying magazine/tv ads).

Build trust: After the first step is done, it’s time to nurture your prospects. If you aren’t a big brand, you probably want to build trust first. Send them some relevant information: Articles, Top X Lists, How-to instructions, etc. After some time, you can sprinkle advertising in your emails. However, if you actually sold to them, don’t stop providing relevant information. This will increase your customer lifetime value.

Conclusion

At first, I was a bit unsure about actually reading this book, because I read Purple Cow by Seth Godin and wasn’t really impressed. Though, this book is impressive. Seth Godin wrote it in 1997 and it was incredible visionary. Today, it is unsurprisingly a bit outdated though the basics are still useful.