public
Created

  • Download Gist
ExcelColumns.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
import Control.Applicative
import Control.Arrow
import Control.Monad
import Control.Monad.Instances
import qualified Data.Vector as V
 
-- Y U NO IN STANDARD LIBRARY????!!!!
if' :: Bool -> a -> a -> a
if' cond x y = if cond then x else y
 
alphabet :: Vector Char
alphabet = V.enumFromTo 'A' 'Z'
nthChar :: Int -> String
nthChar = (:[]) . (alphabet V.!) . (`mod` 26) . (+ 25)
break26 :: Int -> (Int, Int)
break26 = (`divMod` 26)
columnId :: Int -> String
columnId = if' <$> (<= 26) <*> nthChar <*> uncurry (++) . join (***) columnId . break26
main :: IO ()
main = forM_ [1..100] (print . (id &&& columnId))
excel-columns.factor
Factor
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
! A simple program that generates Excel spreadsheet column id corresponding to given column number.
! For example:
! 14 - N
! 26 - Z
! 27 - AA
! 29 - AC
USING: kernel math math.ranges sequences strings ;
IN: excel-columns
 
! Use the following after 'alphabet' definition to make it a compile time constant.
! << \ alphabet alphabet define-constant >>
: alphabet ( -- seq ) CHAR: A CHAR: Z [a,b] ;
: nth-char ( n -- char ) 25 + 26 mod alphabet nth 1string ;
: break-26 ( m -- n o ) 26 /mod ;
: column-id ( i -- str ) dup 26 <= [ nth-char ] [ break-26 [ column-id ] bi@ append ] if ;
 
: test-run ( -- ) 1000 [1,b] [ dup column-id 2array . ] each ;
 
MAIN: test-run

Not sure I would use applicative functors in the columnId function and you probably want to import the Data.Vector as a qualified package.

If it is only to compare with Factor then I would remove the type signatures. ;)

import Control.Applicative
import Control.Arrow
import Control.Monad
import qualified Data.Vector as V

if' cond x y = if cond then x else y

alphabet = V.enumFromTo 'A' 'Z'
nthChar = (:[]) . (alphabet V.!) . (`mod` 26) . (+ 25)
break26 = (`div` 26) &&& (`mod` 26)
columnId = if' <$> (<= 26) <*> nthChar <*> uncurry (++) . join (***) columnId . break26

main = forM_ [1..100] (print . (id &&& columnId))

@DanielOberg: Thanks for the tip about qualified imports! Regarding type signatures, they are for me to make things easy to understand for me. ^-^ As for the use of applicative in columnId, this exercise was purely for the purpose of trying to push the code towards pointfree style and still keep it readable/understandable. By the way, the Factor code above is considered idiomatic, whereas the same cannot be said about the accompanying Haskell code.

As an exercise, can you rewrite break26 so that 26 appears only once in its definition?

There is a tool for it. :D

daniel@Macbook ~ % pointfree -v "\x y -> (y \`div\` x, y \`mod\` x)"
Transformed to pointfree style:
ap (ap . ((,) .) . flip div) (flip mod)
let break26' = (ap (ap . ((,) .) . flip div) (flip mod)) 26

@DanielOberg: Yes, I know about that tool. (It's the same thing as lambdabot's @pl, right?) I meant, can you write something that can be "read"? Sections of (.) don't count (as I said here: http://missingfaktor.blogspot.com/2011/12/sections-of.html). I wrote the columnId definition without any help from lambdabot, and I find it readable. The data flow is easy to visualize. Can we do something like that for break26?

We can simplify it a bit without much effort:

(ap ((&&&) . flip div) (flip mod)) 26

@DanielOberg: That one looks cool. Thanks!

To write break26 while only using 26 once, why not write

let break26 = (`divMod` 26)

@chris-taylor: Thanks. I wasn't aware of that function. Turns out Factor has /mod as well. Will update the snippets accordingly.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.