Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

For a bit of fun, I'd recommend starting off with an ugly bash script when you just want to hackily and happily get something done, then asking chatgpt to convert it to another language(go? perl? ruby?) It is interesting to see the trade-offs.



Seems fun, let's try it.

> ChatGPT> Rewrite the following as concisely as possible in perl, perl 6, J, Haskell, with no explanation and only code:

    history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head
Perl:

    `history | perl -ne '/^\s*\d+\s+(\w+)/ && $a{$1}++; END { for $i (sort {$a cmp $b} keys %a) { print $a{$i}." ".$i."\n"; } }' | sort -rn | head`
Perl 6:

    `history | perl6 -ne '/^\s*\d+\s+(\w+)/ && $a{$1}++; END { for $i (sort keys %a) { say $a{$i}." ".$i; } }' | sort -rn | head`
J:

    `head sort |. |:+/ ,.~ [: ~. stdin -: i.0"1 <@:". ]
Haskell:

    `reverse . take 10 . sort . map (\x -> (length x, head x)) . group . sort . map (words) $ lines <$> readFile ".history"`
from https://www.commandlinefu.com/commands/view/604/list-of-comm...


Both of those Perl solutions could be shorter (and probably quicker) in that you don't need the "sort -rn" as you can do that sort directly in Perl.


Something similar to this;

history | perl -ne '/^\s*\d+\s+(\w+)/ && $a{$1}++; END { map { print "$a{$_} $_\n" } splice((sort {$b <=> $a} keys %a),0,10) }'

Oops, just realised it should be sorted by the value and not the key so...

history | perl -ne '/^\s*\d+\s+(\w+)/ && $a{$1}++; END { map { print "$a{$_} $_\n" } splice((sort {$a{$b} <=> $a{$a}} keys %a),0,10) }'

Also noticed that splice() isn't setting wantarray() so you'll need to use direct indexing which is probably easier to write too...

history | perl -ne '/^\s*\d+\s+(\w+)/ && $a{$1}++; END { map { print "$a{$_} $_\n" } (sort {$a{$b} <=> $a{$a}} (keys %a))[0..9] }'


Usually line noise like that is quickly followed by a "Remote disconnected..." message and I have to dial in again.


Sure, the same thing could be said about Chinese logograms if you don't understand it. Personally I prefer the compact symbology, it's somewhat like Lempel-Ziv tokenisation; it's a form of compression.


For comparison, here the answer from Bing AI

Here are the rewrites in Perl, Perl 6, J and Haskell:

Perl:

> perl -e 'while(<>){$a{$F[1]}++}for(sort{$a{$b}<=>$a{$a}}keys%a){print"$a{$_} $_\n"}' <(history)

Perl 6:

> my %h; for lines() { %h{.[1]}++; }; for %h.sort(-*.value) { .say }' <(history)

J:

> echo 'history' | jconsole -e '10 {. /:~ [: >: [: +/"1 [: =/ [: ;: [: >: [: ;: [: cut ;._2 ] ] ] ] ]'

Haskell:

> import Data.List

> import Data.Ord

> import System.Process

> main = do

    h <- readProcess "history" [] []

    let l = map (head . tail . words) $ lines h

    mapM_ print $ take 10 $ sortBy (flip $ comparing snd) $ map (\x -> (head x, length x)) $ group $ sort l


and answers by phind

# Perl perl -e 'print sort { $b <=> $a } map { /^.\s(.)$/; \$1 } <>' <(history | awk '{a[\$2]++}END{for(i in a){print a[i] " " i}}')

# Perl 6 (Raku) perl6 -e 'lines().map({m/^.\s(.)$/; \$0}).sort({-$^a}).say()' <(history | awk '{a[\$2]++}END{for(i in a){print a[i] " " i}}')

# J history | awk '{a[\$2]++}END{for(i in a){print a[i] " " i}}' | jconsole -je 'echo/:~1!:1]fread'''''

# Haskell ghc -e 'interact $ unlines . take 10 . reverse . sort . lines' <(history | awk '{a[\$2]++}END{for(i in a){print a[i] " " i}}')


I was able to try the perl versions and only the chatgpt is working (I have no experience in perl to check myself)


The Haskell one has a bug, it should be "take10 . reverse . sort". And I suspect the second sort isn't needed but I'm not a Haskell programmer so not as sure about this.




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: