Archive

Posts Tagged ‘Haskell’

Haskell snippet

5 November 2011 Leave a comment

Just some snippet of Haskell code today to return the last but one element of a list. The first implementation uses the built-in functions last and take.

-- Return the last but one element of a list.  This implementation uses         
-- last and take.                                                               
penlast :: [a] -> a
penlast xs = if null xs || length xs <= 2
             then head xs
             else last (take ((length xs) - 1) xs)

The second implementation below uses the built-in functions head and drop.

-- Return the penultimate element of a list.  This implementation uses          
-- head and drop.                                                               
penhead :: [a] -> a
penhead xs = if null xs || length xs <= 2
             then head xs
             else head (drop ((length xs) - 2) xs)
Categories: Haskell, programming Tags: ,

Starting Haskell

9 September 2010 Leave a comment

I’ve started learning Haskell with the free online book Real World Haskell by Bryan O’Sullivan, Don Stewart, and John Goerzen. Exercise 2 in Chapter 2 Types and Functions asks for a function called lastbutone that returns the second last element in a collection of elements. Here’s a first version using conditional:

-- lastbutone-1.hs
-- Return the penultimate element, i.e. the element before the last element.
lastbutone :: [a] -> a
lastbutone xs =
    if (length xs) == 1 || null xs then
       head xs
    else
        head (drop ((length xs) - 2) xs)

And a sample interactive session:

ghci> :load lastbutone-1.hs 
[1 of 1] Compiling Main             ( lastbutone-1.hs, interpreted )
Ok, modules loaded: Main.
ghci> lastbutone [1, 2, 3, 4]
3
ghci> lastbutone [1, 2, 3]
2
ghci> lastbutone [1, 2]
1
ghci> lastbutone [1]
1
ghci> lastbutone []
*** Exception: Prelude.head: empty list

Here’s another version without conditional and using only head, tail, and reverse:

-- lastbutone-2.hs
-- Return the penultimate element, i.e. the element before the last element.
lastbutone :: [a] -> a
lastbutone xs =
    head (tail (reverse xs))

And a sample interactive session:

ghci> :load lastbutone-2.hs 
[1 of 1] Compiling Main             ( lastbutone-2.hs, interpreted )
Ok, modules loaded: Main.
ghci> lastbutone [1, 2, 3, 4]
3
ghci> lastbutone [1, 2, 3]
2
ghci> lastbutone [1, 2]
1
ghci> lastbutone [1]
*** Exception: Prelude.head: empty list
ghci> lastbutone []
*** Exception: Prelude.tail: empty list

Haskell mode spews junk on first load

9 September 2010 Leave a comment

I’m using Haskell mode version 2.8.0 with Emacs 23.1.1 under Ubuntu 10.04.1 LTS. After loading a Haskell file, I would get the following garbage:

#[nil "\300C\207"
      [t]
      2]

Here’s what it looks like:

Acting on a tip from the ArchLinux community, I grep’d through all the Emacs Lisp files in the source distribution of Haskell mode 2.8.0 and found that the garbage in question is output by the following function in haskell-mode.el:

(eval-when-compile
  ;; Emacs 21 defines `values' as a (run-time) alias for list.                  
  ;; Don't maerge this with the pervious clause.                                
  (if (string-match "values"
                    (pp (byte-compile (lambda () (values t)))))
      (defsubst values (&amp;rest values)
        values)))

The culprit is the line

(pp (byte-compile (lambda () (values t)))))

I commented out that whole function and the junk is no longer seen when I first load a Haskell file.