News for hackers. Stuff that matters.
Re:Fun with a Functional Programming Interview Question by dirkt on Sunday February 07, 2021 @02:38AM (#26053377)
Haskell in REPL:

    Prelude> :m Data.List
    Prelude Data.List> let f xs = [(head ys, length ys) | ys <- group xs]
    Prelude Data.List> f "aaaabbbcca"
    [('a',4),('b',3),('c',2),('a',1)]
Of course using the "group" function instead of programming it myself is cheating a bit ...
Re:Fun with a Functional Programming Interview Question by d_t_w on Sunday February 07, 2021 @12:21AM (#26052860)
#(mapv (juxt (comp str first) count) (partition-by identity %))
Re:Fun with a Functional Programming Interview Question by al2o3cr on Saturday February 06, 2021 @09:18PM (#26052037)
Surprising that F# doesn't have a "chunkBy" function for sequences; it makes the solution in Elixir vastly more readable than the version in the article:

    "aaaabbbcca" |> String.codepoints() |> Enum.chunk_by(& &1) |> Enum.map(&{hd(&1), length(&1)})
Re:Fun with a Functional Programming Interview Question by DanielBMarkham on Saturday February 06, 2021 @10:17PM (#26052325)
There is a chunkBy, it's just not this nice. This would be a great project for the F#+ guys.
Re:Fun with a Functional Programming Interview Question by zzo38computer on Sunday February 07, 2021 @02:45AM (#26053399)
I did it this way:

  map (head &&& length) . group
(Although in an actual full program, there would be other considerations (what considerations those are depends on the program being written), and in a full program I probably would not be using Haskell, but rather C.)
Re:Fun with a Functional Programming Interview Question by homic on Sunday February 07, 2021 @01:06AM (#26053043)
I attempted this as a Haskell beginner. Positive it can be improved upon.

    runLengthEncode :: String -> [(Char, Int)]
    runLengthEncode [] = []
    runLengthEncode input =
        let 
            chFirst = head input
            zipped = zip input $ chFirst : input
        in 
            rle zipped (chFirst, 0)
        where 
            rle :: [(Char, Char)] -> (Char, Int) -> [(Char, Int)]
            rle [] (ch, count) = [(ch, count)]
            rle ((ch1, ch2) : cs) (ch, count) = 
                if ch1 == ch2 then
                    rle cs (ch1, count + 1)
                else 
                    (ch2, count) : rle cs (ch1, 1)
Re:Fun with a Functional Programming Interview Question by riwsky on Saturday February 06, 2021 @10:49PM (#26052479)
meanwhile python be out here like:

  [(m.group(1), len(m.group()))
   for m in re.finditer(r"([a-z])\1*", “aaaabbbcca")
Re:Fun with a Functional Programming Interview Question by eesmith on Sunday February 07, 2021 @03:00AM (#26053447)

  import itertools
  [(c, len(list(it))) for (c, it) in itertools.groupby("aaaabbbcca")]
It's not the first time I've wanted

  def ilen(it):
    return sum(1 for _ in it)
as part of itertools. Which would make the second line be:

  [(c, itertools.ilen(it)) for (c, it) in itertools.groupby("aaaabbbcca")]
Rejected many times, eg, from 2010: https://bugs.python.org/issue9547 .
Re:Fun with a Functional Programming Interview Question by badhabit on Tuesday February 09, 2021 @02:15AM (#26074744)
newlisp:

(set 'L '()) (dolist (s (explode "aaaabbbcca")) (if (!= (ref-all s L) '((0 0))) (push (list s 1) L) (inc (L 0 1)))) (print (reverse L))