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

Pages: 1-

ITT we conjure the spirits with our dog

Name: Anonymous 2008-10-11 10:39

mymap :: ( a -> b ) -> [a] -> [b]
mymap fn [] = []
mymap fn list = (fn (head list)) : (mymap fn (tail list))


qsort []     = []
qsort (x:xs) = qsort (filter (< x) xs) ++ [x] ++ qsort (filter (>= x) xs)


Holy Sussman I can see FOREVER

Name: Anonymous 2008-10-11 10:42

void qsort(int a[], int lo, int hi) {
{
  int h, l, p, t;

  if (lo < hi) {
    l = lo;
    h = hi;
    p = a[hi];

    do {
      while ((l < h) && (a[l] <= p))
          l = l+1;
      while ((h > l) && (a[h] >= p))
          h = h-1;
      if (l < h) {
          t = a[l];
          a[l] = a[h];
          a[h] = t;
      }
    } while (l < h);

    t = a[l];
    a[l] = a[hi];
    a[hi] = t;

    qsort( a, lo, l-1 );
    qsort( a, l+1, hi );
  }
}
1

-------------------------------
1- http://www.haskell.org/haskellwiki/Introduction [retrieved on oct. 11, 2008]

Name: Anonymous 2008-10-11 10:56

^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^

Name: Anonymous 2008-10-11 11:26

>>1
Why do you use pattern matching in your qsort but not the map?

mymap _ []     = []
mymap f (x:xs) = f x : mymap f xs

Name: 4 2008-10-11 11:28

>>1
Also, you're needlessly traversing the list twice in the qsort.

qsort []     = []
qsort (x:xs) = qsort lhs ++ [x] ++ qsort rhs
  where
    (lhs, rhs) = partition (< x) xs


The partition function can be found in Data.List.

Name: Anonymous 2008-10-11 11:32

>>4
Because I did the map (haskell noob here), while the qsort is the wiki's demo implementation.

Name: Anonymous 2008-10-11 13:10

haskell = for noobs

Name: Anonymous 2008-10-11 16:30

Are you discussing basic HASKELL functions rewrites?

Name: Anonymous 2008-10-11 17:54

>>8
feels good man

next time we're going to to fibs

Name: Anonymous 2008-10-11 18:15

(define (fib n)
  (fib-iter 1 0 0 1 n))
(define (fib-iter a b p q count)
  (cond ((= count 0) b)
        ((even? count)
         (fib-iter a
                   b
                   (+ (* p p) (* q q))
                   (+ (* 2 p q) (* q q))
                   (/ count 2)))
        (else (fib-iter (+ (* b q) (* a q) (* a p))
                        (+ (* b p) (* a q))
                        p
                        q
                        (- count 1)))))

Name: Anonymous 2008-10-11 18:50

>>8,9,10
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)

Now do the Sieve of Eratosthenes!

Name: Anonymous 2008-10-12 0:11

>>11
In my programming language in which Sieve of Eratosthenes is a primitive function:
Eratosthenes(n)

Name: Anonymous 2008-10-12 7:01

>>12
In my programming language in which Everything is a primitive function:
Everything(n)

Name: Anonymous 2008-10-12 9:21


#define LISTSIZE 10
#include <stdio.h>
#include <math.h>

int main()
{
    int list[LISTSIZE];
    int listMax = (int)sqrt(LISTSIZE), primeEstimate = (int)(LISTSIZE/log(LISTSIZE));

    for(int i=0; i < LISTSIZE; i++)
        list[i] = i+2;

    for(int i=0; i < listMax; i++)
    {
        if(list[i] > 0)
        {
            for(int j = i+1; j < LISTSIZE; j++)
            {
                if((list[j] % list[i]) == 0)
                    list[j] = 0;
            }
        }
    }

    int primesFound = 0;
    printf("Primes for %d integers\n", LISTSIZE);
    for(int i=0; i < LISTSIZE; i++)
    {
        if(list[i] > 0)
        {
            primesFound++;
            printf("-- %d --\n", list[i]);
        }
    }
    printf("Total primes found: %d (estimate was %d)\n", primesFound, primeEstimate);

    return 0;
}

Name: Anonymous 2008-10-12 9:34

primeSieve n = sieve 3 (listArray (1, n) (False : True : cycle [True, False]) :: UArray Integer Bool)
  where sieve i a
          | i > limit = a
          | a ! i     = sieve (i + 1) a'
          | otherwise = sieve (i + 1) a
          where a' = a // zip [i * i, i * i + 2 * i .. n] (repeat False)
        limit = floor (sqrt (fromInteger n))

Name: Anonymous 2008-10-12 10:21

>>15
Not enough ++ and $. Invalid Haskell code.

Name: Anonymous 2008-10-12 13:44

: sqrt s>d d>f fsqrt f>d d>s ;

: sieve ( limit -- table size )
  1+ dup allocate throw 2dup swap true fill
  false over c! false over 1+ c!
  over 4 +do  false over i + c!  2 +loop
  over sqrt 1+ 2 +do
     dup i + c@ if
        over i dup * +do 
           false over i + c! 
        j 2 * +loop
     endif
  loop swap ;

: .table ( table size -- )
  2 +do dup i + c@ if i . endif loop ;

Name: Anonymous 2008-10-13 4:10

>>16
$ and ++ considered harmful!

Name: Anonymous 2010-12-21 6:24


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