Skip to content

Haskell Land

26 December, 2010

After playing around a bit with Haskell (I’ve finished the first few chapters in Learn You a Haskell For Great Good which is awesome, I highly recommend it, even if just for the fun drawings), I’ve finally reached the point where I can write the code for the lists I previously played with in To Curse and Recurse.

So, without further ado, here’s the code (and some other functions as well – these were all part of a Theory of Algorithms homework):

append :: [a] -> [a] -> [a]
append (x:l1) l2 = x : (append l1 l2)
append [] l2 = l2

-- this would take O(n^2) time : (
reverse' :: [a] -> [a]
reverse' (x:l) = append l' [x] -- <- the culprit is here, *append*
        where l' = reverse' l
reverse' [] = []

-- this only takes O(n)
reverse'' :: [a] -> [a]
reverse'' (x:l) = reverse_a l [x]

reverse_a :: [a] -> [a] -> [a]
reverse_a (x:l1) l2 = reverse_a l1 (x:l2)
reverse_a [] l2 = l2

member :: (Eq a) => a -> [a] -> Bool
member a (x:l) = if x == a
                 then True
                 else (member a l)
member a [] = False

size :: [a] -> Integer
size (x:l) = 1 + (size l)
size [] = 0

-- maxelem [] is not defined! it does not make sense
maxelem :: (Ord a) => [a] -> a
maxelem [x] = x
maxelem (x:l) = max x (maxelem l)

-- different 'tail' version
maxelem' :: (Ord a) => [a] -> a
maxelem' (x:l) = maxelem_a l x

maxelem_a :: (Ord a) => [a] -> a -> a
maxelem_a (x:l) m = maxelem_a l (max x m)
maxelem_a [] m = m

minelem :: (Ord a) => [a] -> a
minelem [x] = x
minelem (x:l) = min x (minelem l)

set :: (Eq a) => [a] -> [a]
set (x:l)
    | member x s = s
    | otherwise = x:s
    where s = set l
set [] = []

set' :: (Eq a) => [a] -> [a]
set' (x:l) =
     let s = set l
     in if (member x s)
        then s
        else x:s
set' [] = []

nbrelems :: (Eq a) => [a] -> Integer
nbrelems l = size (set l)

-- from a different set of exercises
-- but really, these comments are worthless : D
remove :: (Eq a) => a -> [a] -> [a]
remove a (x:l)
       | a == x = l'
       | otherwise = x:l'
       where l' = remove a l
remove a [] = []

double :: (Eq a) => a -> [a] -> [a]
double a (x:l) =
       let l' = double a l
        case a == x of
             True -> a:x:l'
             False -> x:l'
double a [] = []

Yes, so congrats to me for reimplementing functions that were already in the Prelude! I feel so very useful 😉 Anyway, this just might be the beginning of a beautiful friendship…

Also, who else is annoyed that Haskell syntax is not supported by WordPress? But, well I’m just thrilled they have syntax highlighting for much more useful and interesting languages like… ColdFusion or ActionScript.

4 Comments leave one →
  1. 27 December, 2010 8:27 AM

    Because they don’t support Haskell I’ve switched to a different alternative: write my post in Vim then use a tool to extract code highlighting, paste it into Vim then paste everything into WordPress. Of course, this is only an idea in its infancy, I don’t have time to finish the HaCoTeB project.

    However, you may use highlight setting it to output to HTML. It supports Haskell.

  2. danf permalink*
    27 December, 2010 10:22 AM

    a. That sounds like a lot of work for some basic syntax highlighting;
    b. I use Emacs 🙂
    c. What is this


    that you speak of?

    Happy holidays 😉 !

  3. 27 December, 2010 12:32 PM

    a is (will be) solved by HaCoTeB if I will finish it. b is not relevant since the editor is only the tool used to produce the content.

    As for c, I guess that I should give some links:



  1. Higher Order Haskell « /Users/dan/proj

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: