Short and sweet

For the Compleat Fan
Locked
Jeremy Dunn
Posts: 95
Joined: Wed Oct 13, 2004 8:02 pm
Location: Bellingham WA

Short and sweet

Post by Jeremy Dunn »

I love it when I find something rediculously simple that I smack myself on the head wondering why it took me so long. Here it is

(define (len=? lst n)(= (length lst)(if n n 1)))

I would say over 90% of the time all I ever do with the length function is to test if a list is equal to a certain amount. You will note that if you don't supply a number the list is tested to see if it has 1 element.

Anyone else have short but sweet function that they have discovered?

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

I like these. Pearls or gems, perhaps, or 'haikus'. I find them hard to write, though.

I wondered whether this would work:

Code: Select all

(define (len=? lst n)(= (length lst)(and 1 n)))

Sammo
Posts: 180
Joined: Sat Dec 06, 2003 6:11 pm
Location: Loveland, Colorado USA

Post by Sammo »

The correct incantation is

Code: Select all

(define (len=? lst n)(= (length lst) (or n 1)))

Locked