Knitting Cables Is Easier Than It Looks

IMG_2599

Last Saturday, I went on a field trip with my knitting guild to Yarn Rhapsody, in Dawsonville Gainesville, GA. While we were there, I was looking at a cable knit sweater, and I remarked to one of my guild mates about how hard it looked. She looked right at me and said, “It’s so easy, you won’t believe it.”

So Wednesday, I decided to try my hand at it. I went to Youtube, and searched for “cable knitting”, and watched the first video that came up. It’s 8:15, and really showed how easy the technique is. After watching it once, I grabbed some cheap worsted weight yarn, a set of US 6 needles, my row counter, and the cable needle that came with the starter kit I bought a couple of months ago, and started knitting.

The actual cable technique is really simple, but the field surrounding the sample from the video does still require accurate counting. I wasn’t paying attention, and ended up completely screwing it up. I ripped it out an decided to try again, but it was late, so I set it aside until I had more time.

And that’s what I did today after work. The sample in the photos took me about two hours, from start to finish. I’m sure that if you were making something like a sweater, where there are parallel cables, that it would be more difficult than this little swatch, since you’d have to keep more numbers in your head, but the basics are simple.

This was fun to make. Here’s the album.

Wee Little Socks

IMG_2583

I got the book Sensational Knitted Socks, by Charlene Schurch, from my local public library. In this book, Ms. Schurch explains the basics of sock knitting, including the parts of a sock, and the various techniques involved in creating them. The first project is what she calls a “class sock”; that is, the sock that she teaches first when she is teaching a knitting class. It’s much smaller than a regular sock, but has all the constituent parts: cuff, leg, heel flap, heel turn, gusset, foot, and toe. You cast on 32 stitches across four DPNs (double-pointed needles), five DPNs, or two sets of circular needles; less than half of what I would need for a sock for myself. I chose to use the four DPNs.

I’ve now knitted three of this sock. The first I did over several days spanning the last two weeks. I would make really good progress, and then something catastrophic would happen. Mostly this involved counting stitches only to discover too many, or too few, in such a way that the pattern was destroyed. Once or twice one of the needles slipped out, and before I could stop it, multiple stitches were heading for the hills. I corrected several problems, but in many cases, it was easier, and more aesthetically pleasing, just to unravel the whole thing and start over. The last time I started that first sock turned out to be the charm, and it took me about ten hours to finish it. It was decent, but had a glaringly obvious purl-where-it-should-have-been-knit row, which I didn’t notice until about ten rows ahead of it.

Lessons learned from this sock:

  • be fanatical about counting stitches
  • be fanatical about checking each row, to ensure the pattern is intact

I was able to do the second sock, from start to finish, in about five hours on Saturday.  The only real issue with this one was a small gap on each side of the gusset. This is a common problem when knitting socks. The biggest accomplishment with this sock was that once I started, I didn’t have to start over. And I knitted where I was supposed to knit, and purled where I was supposed to purl.

I started the third sock on Sunday, and was making fantastic progress, when I got distracted, lost count, lost a stitch, had it escape as I tried to fix it, and ended up unraveling and starting over. I got through the heel turn last night, and then resumed tonight. After a couple of hours, I was finished. I didn’t make any further errors, and it looks like it matches the second one.

Next, I’ll be moving on to an actual pair of socks, that I will actually wear.

You can see the full album at Flickr.

I’ve Taken Up Knitting and Crocheting

About two months ago, I decided to try crochet. I did this, mainly, so I could have a hobby that didn’t make much noise, that I could do while we were watching TV. My Rubik’s Cubes made too much noise, and crochet seemed like it would fit the bill. Shortly after I got started, two of my friends were both extolling the virtues of knitting, so I decided to give that a try, too. One of those friends documents all of his work at Knitting Daddy.

I’ve been mostly alternating between arts: knit one, crochet two, etc. :-) I’ve been posting photos of my finished objects to Flickr, but I am going to try to start posting here about the work, not just pictures of it.

Here’s what I’ve done so far. Details of all of these projects, including patterns and yarn info, can be found on my Ravelry page. I’m joeygibson over there.

First Project: A Knit Scarf

I did several test swatches before actually starting a proper project, but this is it. It took me a week to make.

FullSizeRender 2

Album: https://flic.kr/s/aHskf3eb4m

Second Project: A Knit Crown

This one was a bit silly. Several of my ukulele friends were making them, so I decided to make one, too. This one took a few days.

FullSizeRender 2

Album: https://flic.kr/s/aHskg2beCq

Third Project: Crochet Fingerless Gloves

This one was interesting. I didn’t realize when I started that they were adult smalls, so there was no way they would fit any of us. I continued making them, though, just for fun. I donated them to charity the other day, so maybe someone will get some use out of them. They weren’t supposed to be this big, but I got carried away one night, and did a few too many rows. Rather than frog them, I went ahead and made the other the same length. So they are more like fingerless opera gloves.

IMG_2476

Album: https://flic.kr/s/aHskgiD7Bv

Fourth Project: Knit Potato Chip Scarf

This one took about ten days, but I love the results. I actually bought this yarn before I had a plan for what to do with it. It’s a silk blend that really looks nice up close. I’m going to enter this in the county fair, and then my wife gets to keep it.

IMG_2525

Album: https://flic.kr/s/aHskgriFrd

Fifth Project: Adorable Cthulhu

I saw the pattern for this one and knew I had to make one. The person who posted the pattern said she crocheted it with double strands, but I just did it as a single. I figured since this was my first somewhat complicated project, I didn’t want to complicate it further by having to deal with two strands of yarn. I will make another one, eventually, with two strands.

IMG_2543

Album: https://flic.kr/s/aHskhpJUvr

Odds ‘n’ Ends

These can’t really be called a project, they are just some noodling. I bought a book called Flowers and Leaves: 70 Stitch Patterns You’ll Love To Knit, and I’ve been working some test swatches from there. The first is a little tree.

IMG_2560

Album: https://flic.kr/s/aHski3qHwX

and the second is called “Leaves of Grass”, and sort of looks like grass.

IMG_2567

Album: https://flic.kr/s/aHskeGgH8W

This weekend, I’m going to start on some socks.

LoLClojure – Chapter 3, Continued

I didn’t intend to wait a month between installments, but here we are. When we left off, we were discussing a couple of implementation os exponentiation, and how to print things out. We made it up to page 35.

Next, the book discusses the main Lisp data structure: the list. It discusses how Lisp assumes that in any list, the first item in that list is a command of some sort, and to change that behavior, you place a single quote, ', before the list. Clojure has the same expectation, so that if you enter this at a REPL:

(foo bar baz)

you will get the following error,

CompilerException java.lang.RuntimeException: Unable to resolve symbol: foo in this context...

indicating that it tried to execute that list as a function call, but failed to find a function called “foo”. Just as in Lisp, you can quote the list by prefixing it with a single quote. Thus, this

'(foo bar baz)

is now treated as data, instead of a function call. Similarly, this is a function call

(expt 2 3)

is a call to the expt function, while this

'(expt 2 3)

is a list with three elements.

Barski then goes into a length discussion about the list and its structure, specifically the cons cell. In Lisp, a cons cell is a pair of items, and its list is implemented as a linked list, with the second part of each cons cell linking to the next cons cell in the list. Clojure does not have a cons cell, and so does not implement its lists in terms of them. Lisp uses cons cells individually as simple pairs, too, and you can simulate this use by using a 2-element vector, like so

(def pair [:a :b])

Beginning on page 38, Barski starts discussing the various functions that operate on lists. I will provide the Clojure equivalent functions, where possible.

The cons Function

In Lisp, you create cons cells using the cons function. The result is a cons cell with the two arguments set as the left and right portions of the cell, or just a left-side, if the right is nil. You can simulate this, using the vector function

(vector 'chicken 'cat)

or as a literal vector

['chicken 'cat]

Clojure does have a cons function, but it functions somewhat differently than its Lisp counterpart. It prepends its first argument onto its second argument, which must be a list-like thing, returning the result as a list. So this in Lisp

(cons 'chicken 'cat)

can’t be done using Clojure’s cons function, since the second argument is not a list-like thing. If you try, you will get this error

IllegalArgumentException Don't know how to create ISeq from: clojure.lang.Symbol clojure.lang.RT.seqFrom (RT.java:505)

If you need to create a sequence of a chicken and a cat, use one of these:

(vector 'chicken 'cat)
['chicken 'cat]
'(chicken cat)

Obviously, the first two create vectors, while the last one create a list.

In Lisp, the empty list and nil are equivalent, but not so in Clojure. However, these two examples of consing a value and and empty list, and a value and nil, behave essentially the same, returning a list of just the first element.

(cons 'chicken 'nil) ; (chicken)
(cons 'chicken ())   ; (chicken)

The next three examples function effectively the same, though remember that what you are getting back is not a linked list of cons cells, as you would in Lisp.

(cons 'pork '(beef chicken))           ; (pork beef chicken)
(cons 'beef (cons 'chicken '()))       ; (beef chicken)
(cons 'pork 
      (cons 'beef (cons 'chicken ()))) ; (pork beef chicken)

The car and cdr Functions

Lisp has the basic functions car and cdr for accessing the first element of a lisp, and the remaining elements of a list, respectively. These names are directly related to the original hardware on which Lisp ran. Clojure does not have functions with these names, but it does provide its own functions that give the same results.

Instead of car, Clojure gives us first, which actually is a better name for what it does. It works like this

(first '(pork beef chicken)) ; pork

Instead of cdr, Clojure actually has two functions, next and rest. With a listy thing of 2+ elements, both these function behave the same, returning everything but the first element

(rest '(pork beef chicken)) ; (beef chicken)
(next '(port beef chicken)) ; (beef chicken)

They differ, however, on what happens if there’s nothing after the first element, or the entire list is the empty list. next will return nil in both these cases, while rest will return an empty list. As I said, in Lisp, these are the same thing, but in Clojure, they are very different. The empty list is truthy, while nil is not

(when nil :truthy) ; nil
(when '() :truthy) ; :truthy

The c*r Functions

Lisp defines combinations of car and cdr to allow you to get the second item, the third, the second item from the remainder of the remainder of a list, etc. Most CL implementations provide these functions up to four levels deep. Here’s a partial list to illustrate

(cadr '(pork beef chicken))          ; BEEF
(cdar '((peas carrots tomatoes)
        (pork beef chicken)))        ; (CARROTS TOMATOES)
(cddr '((peas carrots tomatoes)
        (pork beef chicken) duck))   ; (DUCK)
(caddr '((peas carrots tomatoes)
         (pork beef chicken) duck))  ; DUCK
(cddar '((peas carrots tomatoes)
         (pork beef chicken) duck))  ; (TOMATOES)
(cadadr '((peas carrots tomatoes)
          (pork beef chicken) duck)) ; BEEF

And let’s be honest, keeping all those ‘a’s and ‘d’s straight can be pretty confusing (at least, it is to me).

Since Clojure doesn’t have car and cdr, it also lacks these functions. It does have one analog function, and that is for the CL function cadr, which provides the car of the cdr of a list, also known as the second item. Thus, Clojure has second that does the same thing,

(second '(pork beef chicken)) ; beef

but that’s it for built-ins. You can roll your own versions of these CL functions, but from what I’ve read, this is considered a code smell. Even though I don’t think you should try to implement these functions, let me show you how you might do it, if you wanted to.

Instead of explaining each one, and its possible implementation in Clojure, I’m just going to include the code for all of them in one, big blob.

(defn cdar
  [lst]
  (rest (first lst)))

(cdar '((peas carrots tomatoes)
        (pork beef chicken)))       ; (carrots tomatoes)

(defn cddr
  [lst]
  (rest (rest lst)))

(cddr '((peas carrots tomatoes)
        (pork beef chicken) duck))  ; (duck)

(defn caddr
  [lst]
  (first (rest (rest lst))))

(caddr '((peas carrots tomatoes)
         (pork beef chicken) duck)) ; duck

(defn cddar
  [lst]
  (rest (rest (first lst))))

(cddar '((peas carrots tomatoes)
         (pork beef chicken) duck)) ; (tomatoes)

(defn cadadr
  [lst]
  (first (rest (first (rest lst)))))

(cadadr '((peas carrots tomatoes)
          (pork beef chicken) duck)) ; beef

So, if you shouldn’t create analogous functions, how do you get at the elements you need? One way would be to use Clojure’s destructuring. I’m not going to go into a full explanation of how destructuring works, but suffice it to say that it’s a way to assign elements of a listy thing to individual locals.

Here’s how you could achieve the same results as cdar

(let [[fst] '((peas carrots tomatoes) (pork beef chicken))]
  (rest fst)) ; (carrots tomatoes) ; instead of cdar

As you can see, this code assigns the first element '(peas carrots tomatoes) to the local fst. It then calls rest on that, which returns the remainder of that list.

Here, then, is a code dump of destructuring implementation of the rest of the c*r functions that I listed earlier.

(let [[fst] '((peas carrots tomatoes)
              (pork beef chicken))]
  (rest fst)) ; (carrots tomatoes) ; instead of cdar

(let [[_ & rst] '((peas carrots tomatoes)
                  (pork beef chicken) duck)]
  (rest rst)) ; (duck) ; instead of cddr

(let [[_ & rst] '((peas carrots tomatoes)
                  (pork beef chicken) duck)
      tail (rest rst)]
  (first tail)) ; duck ; instead of caddr

(let [[fst] '((peas carrots tomatoes)
              (pork beef chicken) duck)
      tail (rest fst)]
  (rest tail)) ; (tomatoes) ; instead of cddar

(let [[_ & rst] '((peas carrots tomatoes)
                  (pork beef chicken) duck)
      fst (first rst)
      tail (rest fst)]
  (first tail)) ; beef ; instead of cadadr

That’s It, For Now

OK, that brings us to the end of chapter 3. I think this may be the longest post so far. I will move on to chapter 4, and will be back soon with the next installment.

As always, full code is available at https://github.com/joeygibson/lolclojure.

LoLClojure – Chapter 3

Chapter three of Land of Lisp is all about Lisp syntax. This post will be sort of scattered as far as content goes, since the chapter covered a lot. Many things are the same in Clojure, but there are some serious differences. The first is how to define a function.

Defining Functions

In Lisp, you use defun, but in Clojure, it’s defn. Here’s a square function in Lisp.

(defun square (n) 
  (* n n))

And here’s the same function in Clojure. Notice that the function arguments are enclosed in square brackets (it’s a vector), instead of parens.

(defn square
  "Returns the square of the passed-in number"
  [n]
  (* n n))

That string is known as the docstring. It stays with the function, and is available in the REPL by running the doc function, like this (doc square). Lisp also supports docstrings in functions, but it comes after the argument list, instead of before. While docstrings are optional, I highly encourage you to include them. They can span multiple lines, and since they stay with your function, they are useful from the REPL.

Equality

In Lisp, there are may functions for determining equality, and you have to choose the right one for any given circumstance. Among these are eq, equal, equalp, and a few others. In Clojure, there’s just =. If you’re coming from Java, you know = by itself is assignment, not an equality check. For that, you have to use ==, but even that only computes reference equality, and is not always what you want. In Clojure, = does everything you want, in every circumstance. It is your friend.

Exponentiation

Starting on page 34, there are a few examples using the expt function, which raises its first argument to the power of its second. This is a built-in function in CL, but Clojure doesn’t have one. You could use Math.pow from Java, but this only works with doubles, and once the numbers get really large, it switches to scientific notation.

(Math/pow 2 2)       ; 4.0
(Math/pow 2 3)       ; 8.0
(Math/pow 2.0 3)     ; 8.0
(Math/pow 2 10)      ; 1024.0
(Math/pow 53N 53)    ; 2.4356848165022712E91

(In case you haven’t seen it, appending an N to a number literal causes the number to be of type clojure.lang.BigInt. Appending an M makes it a java.math.BigDecimal.)

You can write your own exponentiation function that gives better results than using the one from Java. Here are two different ways to write it. Both versions are tail-recursive, which means they won’t exhaust the stack, but the first uses a nested function, while the second is recursive on a loop. Here’s the nested function version

(defn expt
  "Raise x to the nth power"
  [x n]
  (letfn [(rexpt [n acc]
                 (if (zero? n)
                   acc
                   (recur (dec n) (* acc x))))]
    (rexpt n 1)))

Notice the letfn that contains a local function called rexpt. This function does all the work, and is called as the last line of the main function. It takes a parameter to be used as an accumulator, and this is returned once the exponent is decremented to zero. This nested function is also a closure, because the value of x is referenced directly. We don’t need to change it like we do n, so we just use its name.

Now, here is the version that uses loop. While CL has a loop macro, Clojure’s loop is completely different. All it does is provide a recursion point. This means that when you use the recur function later, execution will jump back to where the loop call is, instead of back to the beginning of the function. The locals declared in the loop’s vector are rebound with the values specified by the recur call. I think this version is easier to understand than the first one.

(defn expt
  "Raise x to the nth power"
  [x n]
  (loop [n n
         acc 1]
    (if (zero? n)
      acc
      (recur (dec n) (* acc x)))))

Notice that the code inside the loop is identical to that in the rexpt local function from the previous example. It’s just not wrapped inside another function. Also of note is in the let we assign n to n. This is a common technique, and will result in a local called n being assigned the value of the passed-in n. The local n can then be decremented with each recursion, without affecting the outer n.

Both of these function provide identical results.

(expt 2 2)       ; 4
(expt 2 3)       ; 8
(expt 2.0 3)     ; 8.0
(expt 2 10)      ; 1024
(expt 53N 53)    ; 24356848165022712132477606520104725518533453128685640844505130879576720609150223301256150373

Notice that passing in an integer results in an integer. Passing in a double results in a double. And passing in a BigInt results in a very large number (Hint: scroll horizontally… it goes on for a while).

Printing Things

CL uses (princ), (prin1), (print), etc., to output things to the console. In Clojure, you use (print) and (println).

(print "He yelled \"Stop that thief!\" from the busy street.") ; no newline
(println "He yelled \"Stop that thief!\" from the busy street.") ; newline

(print) outputs the string, but does not append a newline. (println) appends a newline, as you would expect, given its name.

To Be Continued…

This has gotten very long, so I will stop now, and continue in another post. Stay tuned for Chapter Three, Part Two.

LoLClojure – Locals

Just like in Lisp, Clojure uses let to define locals. The only real difference is that Clojure uses a vector of names and their bindings, whereas Lisp uses a nested list.

This Lisp code

(let ((a 5)
	  (b 6))
  (+ a b))

looks like this in Clojure

(let [a 5
      b 6]
  (+ a b))

I think the Clojure way is a little easier to read.

The biggest difference between the two is when it comes to local functions. CL has flet for defining local functions, and labels for defining local functions that need to be able to call each other (or call themselves recursively). Here’s an example of each

;; A single local function
(flet ((f (n)
		  (+ n 10)))
  (f 5))

;; Two local functions that don't need to call each other
(flet ((f (n)
		  (+ n 10))
	   (g (n)
		  (- n 3)))
  (g (f 5)))

Here’s the equivalent code in Clojure. Note how the square brackets make things stand out a little bit more.

(letfn [(f [n]
           (+ n 10))]
  (f 5))

(letfn [(f [n]
           (+ n 10))
        (g [n]
           (- n 3))]
  (g (f 5)))

If the functions need to reference each other, in CL you have to use labels, instead of flet. Here’s how that looks (the only difference is the form used; the arguments remain the same)

(labels ((a (n)
			(+ n 5))
		 (b (n)
			(+ (a n) 6)))
  (b 10))

In Clojure, you don’t need to use anything other than letfn, because it already supports the recursive nature that labels provides

(letfn [(a [n]
           (+ n 5))
        (b [n]
           (+ (a n) 6))]
  (b 10))

Finally, if you have local functions and other local bindings you need to establish, you can use a let, but no recursion is supported. This is sort of like CL’s flet but you can also use it for binding locals that are not functions

(let [a (fn [n]
          (+ n 5))
      b (fn [n]
          (+ (a n) 6))
      c 10]
  (b c))

I think the way Clojure uses square brackets in certain places that CL uses parentheses makes the code easier to read, overall.

LoLClojure – Land of Lisp In Clojure

I read Conrad Barski’s excellent book Land of Lisp a couple of years ago, and worked through all the examples using CLisp, but I thought it might be fun to go through it again, but use Clojure instead. Other people have done it already, but what’s one more, eh?

As I work through the book, I will be putting all the code on Github at https://github.com/joeygibson/lolclojure

So, the first example is for a program to guess a number you are thinking of. In Lisp, defparameter allows you to rebind values, but Clojure’s def is immutable. Using a ref gets around this, though it is a bit clunky (since refs are intended for multi-threaded access.) The code is not great, and you wouldn’t write a Clojure program like this (or a Lisp program, really); it’s just to get the discussion moving. Better code is coming.

Anyway, here’s the number-guessing program in non-idiomatic Clojure. To run it, load it into a REPL, then execute (guess-my-number). If you are so enraptured with the game that you want to play it again, execute (start-over) and then (guess-my-number).

(ns lolclojure.guess)

;; Using refs for these is overkill, but the original
;; used mutable global variables.
(def small (ref 1))
(def big (ref 100))

(defn guess-my-number
  "This is, effectively, a binary search between the two extremes."
  []
  (Math/round (Math/floor (double (/ (+ @small @big) 2)))))

(defn smaller
  "The guess was too high, so lower it."
  []
  (dosync
   (ref-set big (dec (guess-my-number)))))

(defn bigger
  "The guess was too low, so raise it."
  []
  (dosync
   (ref-set small (inc (guess-my-number)))))

(defn start-over
  "Reset everything and prepare to start over."
  []
  (dosync
   (ref-set small 1)
   (ref-set big 100)))