# Bit Of Fun In Haskell

Page 1 of 1

## 5 Replies - 1498 Views - Last Post: 05 February 2011 - 09:12 AM

### #1 Shane Hudson

• D.I.C Technophile

Reputation: 345
• Posts: 1,286
• Joined: 06-December 09

# Bit Of Fun In Haskell

Posted 15 January 2011 - 03:45 AM

I am incredibly new to Haskell, but having got to the triangle at the bottom of the first chapter of Learn You A Haskell - which explained how to get all combinations of triangles with sides up to 10 - I had an idea. "What if we could do the same but for words?"

So here it is:

```let words = [ (a,b,c) | a <- ['a'..'z'], b <- ['a'..'z'], c <- ['a'..'z'] ]

```

Many of you may already know about it, but I have had great fun with it and hope others will too. If you do not understand what it is, basically it gives you every possible combination for a word with 3 letters.

EDIT:

Here is a screenshot!

This post has been edited by Shane Hudson: 15 January 2011 - 04:47 AM

Is This A Good Question/Topic? 1

## Replies To: Bit Of Fun In Haskell

### #2 Raynes

• D.I.C Lover

Reputation: 614
• Posts: 2,815
• Joined: 05-January 09

## Re: Bit Of Fun In Haskell

Posted 15 January 2011 - 04:16 AM

Neat concept. This is the kind of code that blows imperative programmer's minds. I'm happy you're having fun.

Here is my Clojure version just for fun, all done in the REPL:

```user=> (take 10 words)
("aaa" "aab" "aac" "aad" "aae" "aaf" "aag" "aah" "aai" "aaj")
user=> (def alphabet "abcdefghijklmnopqrstuvwxyz")
#'user/alphabet
user=> (def words (for [a alphabet b alphabet c alphabet] (str a b c)))
#'user/words
user=> (take 10 words)
("aaa" "aab" "aac" "aad" "aae" "aaf" "aag" "aah" "aai" "aaj")

```

Clojure doesn't have a range that works for characters, so I had to define the alphabet there.

This post has been edited by Raynes: 15 January 2011 - 07:01 AM

Was This Post Helpful? 1

### #3 Shane Hudson

• D.I.C Technophile

Reputation: 345
• Posts: 1,286
• Joined: 06-December 09

## Re: Bit Of Fun In Haskell

Posted 15 January 2011 - 04:51 AM

Raynes, on 15 January 2011 - 11:16 AM, said:

Neat concept. This is the kind of code that blows imperative programmer's minds. I'm happy you're having fun.

It sure does, I love most of the languages I have used. But this is just something else! Not quite sure yet how I will use it for anything useful, but I expect I will pick some of that up when I get onto Real World Haskell.

I am surprised Clojure does not have the ranges, even Excel does! I see you only took some of the words, didn't want to run the whole thing? Haha, for 4 letters it takes forever! Three letters takes about 5 mins for me in Haskell.
Was This Post Helpful? 0

### #4 Raynes

• D.I.C Lover

Reputation: 614
• Posts: 2,815
• Joined: 05-January 09

## Re: Bit Of Fun In Haskell

Posted 15 January 2011 - 06:57 AM

Clojure does have integer ranges:

```user=> (doc range)
-------------------------
clojure.core/range
([] [end] [start end] [start end step])
Returns a lazy seq of nums from start (inclusive) to end
(exclusive), by step, where start defaults to 0, step to 1, and end
to infinity.
nil

```

And, using the char and int functions, you could use range to generate ranges of characters. However, I agree that Clojure could use a char-range function specifically for generating character ranges, so I submitted a patch to add it. Here's hoping!

For the time being, here is how I implemented it in case anybody wants to use it:

```(defn char-range
"Returns a lazy seq of chars from start to end
(inclusive), by step, where start defaults to Character/MIN_VALUE,
step to 1, and end to Character/MAX_VALUE."
{:added "1.3"
:static true}
([] (char-range (Character/MIN_VALUE) (Character/MAX_VALUE) 1))
([end] (char-range (Character/MIN_VALUE) end 1))
([start end] (char-range start end 1))
([start end step]
(map char (range (int start) (inc (int end)) step))))

```

It works almost precisely the same as the original range function.

EDIT: Just noticed I said that Clojure didn't have a numerical range function, but I meant a range function that works on characters. My bad.

This post has been edited by Raynes: 15 January 2011 - 06:59 AM

Was This Post Helpful? 0

### #5 Shane Hudson

• D.I.C Technophile

Reputation: 345
• Posts: 1,286
• Joined: 06-December 09

## Re: Bit Of Fun In Haskell

Posted 05 February 2011 - 08:28 AM

Just been trying to refactor the code a little, here is what I came up with... if anyone knows a better way let me know!

```let all = ['a'..'z']
let letters = [(a,b,c) | a <- all, b <- all, c <- all]
-- If you want to save to a text file (probably more convenient than just printing to the screen
writeFile "letterCombinations"
(show letters)

```

Was This Post Helpful? 0

### #6 Raynes

• D.I.C Lover

Reputation: 614
• Posts: 2,815
• Joined: 05-January 09

## Re: Bit Of Fun In Haskell

Posted 05 February 2011 - 09:12 AM

Looks good to me.
Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }