Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Elegant solutions

Name: Anonymous 2007-06-28 15:16 ID:lcs47ZU6

Im bored, show me some good code (any language whatsoever)
Post a problem, then a great solution.

1) Transpose a matrix
(apply #'mapcar #'list '(( 1  2  3  4)
                         ( 5  6  7  8)
                         ( 9 10 11 12)
                         (13 14 15 16)))
(( 1  5  9 13)
 ( 2  6 10 14)
 ( 3  7 11 15)
 ( 4  8 12 16))

Name: Anonymous 2007-06-28 15:24 ID:ERvjIcVI

2) kill a computation if it doesn't terminate after <limit> microseconds

compute_ :: Int -> IO a -> IO (Maybe a)
compute_ limit computation = do
    result <- atomically newEmptyTMVar
    runner <- forkIO $ do c <- computation
                          atomically $ putTMVar result $ Just c
    reader <- forkIO $ do threadDelay limit
                          killThread runner
                          atomically $ putTMVar result $ Nothing
    a <- atomically $ takeTMVar result
    killThread runner
    killThread reader
    return a


*Main> let loop = loop in compute_ 1000000 loop
Nothing

Name: Anonymous 2007-06-28 16:02 ID:9cNOan1x

matrix multiplication.
I wrote a simple lisp code for this, but I want to see other options.

Name: Anonymous 2007-06-28 16:07 ID:Heaven

1) Transpose a matrix
>>> mat = (1, 2, 3, 4), (5, 6, 7, 8), (9, 10, 11, 12), (13, 14, 15, 16)
>>> tuple(zip(*mat))
((1, 5, 9, 13), (2, 6, 10, 14), (3, 7, 11, 15), (4, 8, 12, 16))

Name: Anonymous 2007-06-28 16:11 ID:CbPgANVr

3) Running a program for an undefined amount of time
while (true);

Name: Anonymous 2007-06-28 16:14 ID:Heaven

in before factorials with haskell

Name: Anonymous 2007-06-28 16:16 ID:Heaven

>>5
ahahaha

Name: Anonymous 2007-06-28 16:19 ID:lcs47ZU6

op here, i'd write it like this.


(mapcar (lambda (row)
 (apply #'mapcar (lambda (&rest column) (apply #'+ (mapcar #'* row column))) matrix2)) matrix1)

Name: Anonymous 2007-06-28 16:23 ID:lcs47ZU6

(mapcar
 (
lambda (row) (apply (function mapcar)
   (
lambda (&rest column)
    (
apply (function +) (mapcar (function *) row column))) matrix2)) matrix1)

Name: Anonymous 2007-06-28 16:24 ID:lcs47ZU6

(mapcar
 (
lambda (row)
  (
apply (function mapcar)
   (
lambda (&rest column)
    (
apply (function +) (mapcar (function *) row column))) matrix2)) matrix1)

Name: Anonymous 2007-06-28 16:31 ID:Heaven

matmul a b = map (\r -> map (sum . (zipWith (*) r)) (transpose b)) a

Name: Anonymous 2007-06-28 16:37 ID:9cNOan1x

>>8
better than my version. I transposed the second matrix first.
(defun dot-product (l1 l2)
    (apply #'+ (mapcar #'* l1 l2)))
(defun row-by-columns (row columns)
    (mapcar (lambda (c) (dot-product row c)) columns))
(defun matrixm2 (matrix1 matrix2)
    (let ((tr (apply #'mapcar #'list matrix2)))
        (mapcar (lambda (f) (row-by-column f tr)) matrix1))) 

If I've read your code well, you're inserting the apply mapcar directly.

Name: Anonymous 2007-06-28 16:40 ID:Heaven

also, transpose
transpose [x] = map (:[]) x
transpose (x:xs) = zipWith (:) x (transpose xs)

Name: Anonymous 2007-06-28 16:43 ID:Heaven

better version
transpose [] = repeat []
transpose (x:xs) = zipWith (:) x (transpose xs)

Name: Anonymous 2007-06-28 17:05 ID:9cNOan1x

>>11
great solution!
this is how it looks in lisp

(defun matrixm3 (a b)
    (mapcar (lambda (r) (mapcar (compose #'sum (p/ mapcar (function *) r)) (transpose b))) a))

(defun sum (l)
    (apply #'+ l))
(defun transpose (m)
    (apply #'mapcar #'list m))

of course, you need to have defined compose and p/, which is partial aplication.

Name: Anonymous 2007-06-28 17:26 ID:Heaven

>>14
Now with 100% more foldr

tranpose = foldr (zipWith (:)) (repeat [])

Name: Anonymous 2007-06-28 18:29 ID:lcs47ZU6

>>16
very slick, its even written in pointless style.
next challenge: uniq
e.g. 1 2 3 3 3 4 5 8 6 6 6 8 9 5 5 5 -> 1 2 3 4 5 8 6 8 9 5

Name: Anonymous 2007-06-28 19:09 ID:FtQRFguh

main(){system("uniq");}

Name: Anonymous 2007-06-28 19:30 ID:8ZraZvvg

Determine if a year is a leap year.

int isLeap(int year){return(year%400)?(year%100)?(year%4)?0:1:0:1;}

Name: Anonymous 2007-06-28 20:04 ID:HMaDqBI7

>>17
<?php
$n = "1 2 3 3 3 4 5 8 6 6 6 8 9 5 5 5";
$u = array();
$m = explode( ' ',  $n );
$r = '';

foreach( $m as $v )
{
    if( !$u[ $v ] )
    {
        $u[ $v ] = true;
        $r .= $v . ' ';
    }
}

echo "This list, {$n}, is now unique: ";
echo substr( $r, 0, -1 ); //remove trailing space
?>

Name: Anonymous 2007-06-28 20:05 ID:HMaDqBI7

...forgot some EXPERT [ code ]s

<?php
$n = "1 2 3 3 3 4 5 8 6 6 6 8 9 5 5 5";
$u = array();
$m = explode( ' ',  $n );
$r = '';

foreach( $m as $v )
{
    if( !$u[ $v ] )
    {
        $u[ $v ] = true;
        $r .= $v . ' ';
    }
}

echo "This list, {$n}, is now unique: ";
echo substr( $r, 0, -1 ); //remove trailing space
?>

Name: Anonymous 2007-06-28 20:17 ID:Heaven

>>20
>>21
fail

Name: Anonymous 2007-06-28 21:43 ID:W4zqoxYd

In Python, and actually elegant, not crappy:
i, d = '1 2 3 3 3 4 5 8 6 6 6 8 9 5 5 5', {}
print 'This list, %s, is now unique:' % i
print ' '.join(d.setdefault(n, n) for n in i.split() if n not in d)

Or if you don't care about preserving order:
i = '1 2 3 3 3 4 5 8 6 6 6 8 9 5 5 5'
print 'This list, %s, is now unique:' % i
print ' '.join(sorted(set(i.split())))

Name: Anonymous 2007-06-28 23:02 ID:Heaven

>>23
ONE WORD! FORCED INDENTATION OF THE CODE! THREAD OVER!

Name: Anonymous 2007-06-28 23:18 ID:9cNOan1x

>>23
are you a troll, or just a dumbass? you were supposed to CODE the "set" function! the set function, in fact, it's all you were supposed to code.

there are many ways of writing "uniq", depending if you want efficience or not, or if you want to preserve the order, or not.

Name: Anonymous 2007-06-29 0:19 ID:1AXGUeS5

read lines from file into list or array of strings

(with-open-file (s "test.c")
  (loop for line = (read-line s nil :eof)
        until (eq line :eof)
        collect line))

Name: Anonymous 2007-06-29 0:21 ID:1AXGUeS5

>>2
is that Haskell?

Name: Anonymous 2007-06-29 0:22 ID:1AXGUeS5

>>5
for (;;);

Name: Anonymous 2007-06-29 2:21 ID:TGav32jf

Fork-bomb:
main(){while(1)fork();}
Let's see you suave LISP faggots do that.

Name: Anonymous 2007-06-29 4:30 ID:/T2FVqR7

>>17
uniq [] = []
uniq (x:xs) = x : uniq (dropWhile (== x) xs)

Name: Anonymous 2007-06-29 5:54 ID:GGxG1Je+

>>26
lol hi, heres how a expert BBCode programmer would do it in C:

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>

char ** read_lines(char * filename) {
  char ** output, * p, ** q;
  struct stat file_stat;
  FILE * fptr = fopen(filename, "r");
 
  output = malloc(fread(p = malloc(fstat(fileno(fptr), &file_stat) + file_stat.st_size), 1, file_stat.st_size, fptr)*sizeof(char *));
  *(q = output) = p;
  do do if(*p == '\n') *p = '\0'; while(*p++); while((*q++ = p) && *p);
  *q++ = NULL;
 
  return output;
}

void print_lines(char ** lines) {
  while(*lines)
    printf("<%s>\n", *lines++);
}

int main(void) {
  char * filename = "test.c";
  print_lines(read_lines(filename));
  return 0;
}

Name: Anonymous 2007-06-29 6:15 ID:GGxG1Je+

>>29
lisp fork bomb cons
#1=(#1# . #1#)

Name: Anonymous 2007-06-29 6:16 ID:HOdGUDqA

>>30
Only works if all the duplicate values are together. For the general case you'll want to use filter.

Name: Anonymous 2007-06-29 6:47 ID:TGav32jf

>>32
Nope, that's cheating. Nice try though.

Name: Anonymous 2007-06-29 7:39 ID:oz4sORyk

>>30

(oncdrs (cons it (remove-if (p/ = it) rec) nil lst)

but this is a bit inefficient, cause you filter after the recursion.
if you don't care about the order:

(oncdrs (if (member it rec) rec (cons it rec)) nil lst)




Name: Anonymous 2007-06-29 8:09 ID:Heaven

>>25
set() is not order preserving

Name: Anonymous 2007-06-29 9:11 ID:uYh3AZ/6

>>33
Just sort the input first. It is faster.

Name: Anonymous 2007-06-29 9:16 ID:A5mpTx2G

>>32
too much syntax.
fork bomb in factor:
USING: threads ;
[ dup in-thread ] dup in-thread


Name: Anonymous 2007-06-29 13:54 ID:/T2FVqR7

37) List of all prime numbers
let primes (x:xs) = x : primes (filter ((/= 0) . (flip mod x)) xs) in primes [2..]

Name: Anonymous 2007-06-30 21:04 ID:bXwJRXAJ

>>39
let primes = List.nubBy (((== 0) . ) . flip mod) [2..]

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List