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

Point-free programming style

Name: Anonymous 2009-01-04 19:30

How do I write the factorial function in point-free?

fact n = foldr1 (*) [1..n]

Which is faster
add1 = 1 +
or
add1 x = 1 + x
???

Name: Anonymous 2009-01-04 19:35

>>1
fact = product.enumFromTo 2

Name: Anonymous 2009-01-04 20:31

>>1-2
Neither of those work for negative numbers.

Also, >>1, your first add1 is a syntax error.

Name: Anonymous 2009-01-04 20:55

>>3
Factorials are only defined for non-negative integers, but even so:

ghci> product.enumFromTo 2 $ negate 5
1


Which is the product of all positive integers less than or equal to (-5), i.e. the empty product.

Name: Anonymous 2009-01-04 21:10

fact = gamma.(+) 1

i'll leave implementing the gamma function to you.

Name: Anonymous 2009-01-04 21:10

how do i implemented factorial in O(n(log n log log n)2) time in haskell?

Name: Anonymous 2009-01-04 21:42

What rolls down stairs alone or in pairs
Rolls over your neighbor's dog?
What's great for a snack and fits on your back?
It's Log, Log, Log!

Name: Anonymous 2009-01-04 21:45

Haskell the Dog for the last one

Name: Anonymous 2009-01-04 22:24

>>7
from blammo!

Name: Anonymous 2009-01-04 22:55

Rolls over your neighbor's dog?
TIRES

Name: Anonymous 2009-01-05 8:41

Point-free and memoizing:

import Control.Monad.Instances
import Monad
import List

facs = snd $ mapAccumL ((join (,) .) . (*)) 1 $ 1: [1..]
fac = (fac !!)

Name: FrozenVoid !FrOzEn2BUo 2009-01-05 8:49

bench(loop,factorial,10000000,45)=567ms
10000000 loops in 567ms.
Can your Haskell do this?
function factorial(x){for(var i=x-1;i>0;i--){x*=i};return x}

function bench(x,y,z,z1,z2,z3,z4,z5,z6,z7,z8,z9,z10){var d=new Date();var std=d.getTime();var res=x(y,z,z1,z2,z3,z4,z5,z6,z7,z8,z9,z10);var d2=new Date();var std2=d2.getTime();
var diff=std2-std;return diff}
function loop(x,y,z,z1,z2,z3,z4,z5,z6,z7,z8,z9,z10){for(var i=y-1;i>-1;i--){var res=x(z,z1,z2,z3,z4,z5,z6,z7,z8,z9,z10)}}

Name: Anonymous 2009-01-05 9:26

>>12
In Haskell that takes 0ms because there's no need to evaluate any of it since the computation isn't actually used for anything.

Name: FrozenVoid !FrOzEn2BUo 2009-01-05 9:30

>>13
Wow that fails even more. Is there any benchmarking progs?

Name: Anonymous 2009-01-05 9:45

>>11

facts = 1: zipWith (*) facs [1..]
fact = (facts !!)

Name: FrozenVoid !FrOzEn2BUo 2009-01-05 9:47

>>15
It isn't obvious how fast it runs.

Name: Anonymous 2009-01-05 9:49

facts = 1: zipWith (*) facts [1..]
fact = (facts !!)

Name: FrozenVoid !FrOzEn2BUo 2009-01-05 9:52

>>17
well, if Haskell lacks facilities to express time in milliseconds or other formats just say it.

Name: Anonymous 2009-01-05 9:57

FUCK OFF TRIPFAG
YOU POST LIKE A SMALL CHILD
AND YOUR PRESENCE HERE HAS BEEN WHOLLY UNSATISFYING
LEAVE /PROG/ FOREVER

Name: FrozenVoid !FrOzEn2BUo 2009-01-05 10:00

>>19
My posting style is irrelevant.
There isn't anything equivalent (to /prog/) which doesn't suck.

Name: Anonymous 2009-01-05 10:15

>>18
In Haskell time is expressed in dog years.

Name: Anonymous 2009-01-05 10:21

pointless programming

Name: Anonymous 2009-01-05 12:44

>>22
pointless posting

Name: Anonymous 2009-01-05 12:48

>>20
Can't you just leave?
This obviously is not the place for you.
Yes, you can benchmark stuff in haskell.
Yes, haskell is fast.
Fucking use google.

Name: FrozenVoid !FrOzEn2BUo 2009-01-05 12:53

>>24
If i can buy a membership card for this club, could i stay?

Name: Anonymous 2009-01-05 12:55

>>20
/prog/ sucks.

Name: FrozenVoid !FrOzEn2BUo 2009-01-05 12:57

>>26
Its archives posts from 3 years ago.
It has more programming topics then /g/(/g/ Quality is down recently).
Its threads do not expire and easier to track.

Name: Anonymous 2009-01-05 13:00

>>27
Its archives posts from 3 years ago.
So does every other BBS software.

It has more programming topics then /g/
It also has more programming topics than /a/. What's your point?

/g/ Quality is down recently
There was never any quality to be found on /g/.

Its threads are easier to track.
Maybe you should try the 4chan Firefox extension.

tl;dr back to /g/, please

Name: FrozenVoid !FrOzEn2BUo 2009-01-05 13:02

>>28
Show me a /prog/ equivalent which doesn't as >>20

Name: Anonymous 2009-01-05 13:03

>>28
DON'T SHOW HIM!

Name: Anonymous 2009-01-05 13:04

Shame !! is still O(n), so unless I misunderstood something, memorizing it is still less than perfect. Naturally, the facts lazy list is fast.

function bench(x,y,z,z1,z2,z3,z4,z5,z6,z7,z8,z9,z10){var d=new Date();var std=d.getTime();var res=x(y,z,z1,z2,z3,z4,z5,z6,z7,z8,z9,z10);var d2=new Date();var std2=d2.getTime();
var diff=std2-std;return diff}
function loop(x,y,z,z1,z2,z3,z4,z5,z6,z7,z8,z9,z10){for(var i=y-1;i>-1;i--){var res=x(z,z1,z2,z3,z4,z5,z6,z7,z8,z9,z10)}}

var res=x(z,z1,z2,z3,z4,z5,z6,z7,z8,z9,z10)
z10
___/ |/ _ [br]|_  / | | | |
/ /| | |_| |
/___|_|\___/


Fuck, I RAGED. You have not achieved satori. Come back when you have learnt to use arguments/apply.

Name: FrozenVoid !FrOzEn2BUo 2009-01-05 13:05

>>30
What are you afraid of?

Name: FrozenVoid !FrOzEn2BUo 2009-01-05 13:07

>>31
I don't use the [arguments] array since that would be inelegant and increase code size atleast x2(plus its slower).

Name: Anonymous 2009-01-05 13:10

Name: FrozenVoid !FrOzEn2BUo 2009-01-05 13:17

I don't understand >>34
Its summing integers inline? or there some subtle context?

Name: FrozenVoid !FrOzEn2BUo 2009-01-05 13:45

>>34
Its seems to calculate sum of excel cells. waste of variables.
bench() doesn't use arguments array because the code would
include a slower for(;;) and conditional which would made the benchmarks slower.
 It needs to pass these 10 arguments millions of times.
Speed in benchmarking is critical.
Without z1-z10 ,parsing arguments array 10 millions times would be way slower then my optimized functions, completely ruining the purpose of bench().

Name: Anonymous 2009-01-05 13:59

>>37
The stack is an "array" too, it requires memory access unless you pass arguments as registers, that said, passing 10 arguments directly will use the stack on a x86, as you can only pass 2-3 arguments as registers, so you implementation would be either slower or have similar speed to one using an args array. If you really cared about speed of your benchmarking function, you would have coded it in asm, and used the CPU instruction which returns ticks, if such a thing exists on your platform( rdtsc instruction for x86), and probably would have inlined the entire benchmark function too.

Name: FrozenVoid !FrOzEn2BUo 2009-01-05 14:04

I'm interested in programming in assembler.
However there certain features (like size of code),memory management and some opcodes i can't understand.

Name: Anonymous 2009-01-05 14:18

>>38
You'll have to be more specific with your questions, I don't understand what you mean by ``size of code".
As for memory management, if you're writting an OS, or something similar ( like a debugger which doesn't depend on the OS ), you'll have to do the memory management yourself by modifying the needed descriptors and tables, details for this can be found in the fine manuals, otherwise, you should just call whatever APIs or library functions are provided to you by the OS to do the allocation of freeing of memory. Can't answer the third question since you gave no details, but I'm sure reading the fine manuals will give you the answer.

Name: FrozenVoid !FrOzEn2BUo 2009-01-05 14:27

>>39
[size of code]
how much text would factorial+bench+loop take in asm?
I assume about 5 times. They could be more optimized but you have to maintain alot more code.

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