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

Pages: 1-4041-

[SICP] Ex 1.30 / 1.31

Name: Anonymous 2008-02-10 7:46

Here's what I have

; Ex 1.30
(define (sum term a next b)
  (define (iter a result)
    (if (> a b)
        result
        (iter (next a) (+ result (term a)))))
  (iter a 0))
; Ex 1.31
(define (product term a next b)
  (define (iter a result)
    (if (> a b)
        result
        (iter (next a) (* result (term a)))))
  (iter a 1))

(define (fac n)
  (product (lambda (x) x) 1 (lambda (x) (+ x 1)) n))


Am i rite?

Name: Anonymous 2008-02-10 8:01


unsigned sum(unsigned (*term)(unsigned), unsigned a, unsigned b, unsigned (*next)(unsigned)) {
    unsigned ret = 0;
    while(b > a) {
        ret += term(a);
        a = next(a);
    }
    return ret;
}

BEAUTIFUL

Name: Anonymous 2008-02-10 8:05

>>1
Yeah you are.
Also, I'm wondering if anyone can write that in Haskell or Factor.
I seriously doubt it's even possible in the latter.

Name: Anonymous 2008-02-10 8:09

>>3

mysum term a next b =
   if (a > b)
      then 0
      else (term a) + (mysum term (next a) next b)

Well THAT WAS HARD

Name: Anonymous 2008-02-10 9:07

>>4
That's Haskell, not Factor.

Name: Anonymous 2008-02-10 9:33

>>5
I'm wondering if anyone can write that in Haskell
Yes, they can.
That's Haskell, not Factor.
No one cares about Factor.

Name: Anonymous 2008-02-10 9:49

>>4
Fail, that is not tail-recursive.

Name: Anonymous 2008-02-10 9:53

>>6
>>3 emphasized that it might be impossible to write it in Factor. Everyone knows how to do it in Haskell, he was expecting someone to prove him wrong about the Factor part.

I think.

Name: Anonymous 2008-02-10 9:58

>>1
What is all that load of inefficientn junk, what the fuck dude?
Did you seriously had to count all that ))))) to write a sum function?


long sum(int iArray[]) {
    int iSize = strlen(iArray);
    int iCount;
    long lSum;

    for(iCount = 0; iCount < iSize; iCount++)
        lSum += iArray[iCount];

    return (lSum);
}

Name: Anonymous 2008-02-10 10:05

>>9
>int iSize = strlen(iArray);

WOW

Name: Anonymous 2008-02-10 10:10

>>10
Oh fuck
replace it with

int iSize = strlen(iArray) + 1;

I keep forgetting this for arrays -_-

Name: Anonymous 2008-02-10 10:20

>>9
Hungarian notation considered filthy.

Name: Anonymous 2008-02-10 10:30

>>12
Nobody cares.

Name: Anonymous 2008-02-10 10:37

>>11
Never program in C again.

Name: Anonymous 2008-02-10 11:55

>>9
I can't believe the amount of failure I'm seeing in your post.

- You don't understand Scheme. Your program doesn't do anything like the OP's one.
- You don't understand Hungarian notation. No, it's not about prefixing types to variable names. Seriously.
- You don't understand C. That has already been pointed out.
- You don't understand text editors. We don't need to count parentheses.

Still, you're trying to be all cocky at someone obviously better than you. Good job!

Name: Anonymous 2008-02-10 13:10

>>9
Please be trolling.

Name: Anonymous 2008-02-10 15:55

I can't believe THE BLOODY AMOUNT of failure I'm seeing in your POST.

- You DON'T understand Scheme. Your program doesn't do CRACKER anything like the BLOODY KNOCK UP OP's one.
JOHN - YOU don't understand Hungarian notation. NO, it's CHOAD not ASSWIPE about prefixing TYPES to variable names. Seriously.
- You don't understand C. FRENCH TICKLER That has ALREADY been pointed out.
- You don't understand text editors. We don't need to count PARENTHESES.

Still, YOU'RE trying to be ALL RUMP-SHAKER cocky at someone obviously better than you. Good job!

Name: Anonymous 2008-02-10 17:21

I COCK can't believe the amount of failure I'm BLOODY HELL seeing in YOUR post.

- You don't QUEEF UNDERSTAND Scheme. Your program doesn't do THREEWAY WOMAN anything like TAR BABY THE SODDING OP's GET BENT one.
- You don't understand Hungarian TEA ROOM notation. RIM JOB No, IT'S not about prefixing GLORY HOLE types to BEAR variable HUNG names. Seriously.
- You don't understand C. That DYKE has already been BEAVER pointed OUT.
- You don't understand BEAVER text editors. We don't need BALLS TO count parentheses.

Name: Anonymous 2008-02-10 19:32

That's not general enough:

(define (curry f . curried)
  (lambda args
    (apply f (append curried args))))

(define (accumulate acc-function neutral end-test term from next end)
  (define (iter from result)
    (if (end-test from end)
        result
        (iter (next from) (acc-function result (term from)))))
  (iter from neutral))

(define sum (curry accumulate + 0 >))
(define product (curry accumulate * 1 >))


Name: Anonymous 2008-02-10 20:10

>>15
YHBT

Name: Anonymous 2008-02-10 22:43

>>20
I disagree, for it is really you who HBT.

Name: Anonymous 2008-02-11 5:12

I.
INVENTED.
TROLLING.

Name: Anonymous 2008-02-11 5:26

>>19
That is not general enough either.
With OPs code you can return the sum of all odd numbers 1..100 like this

(define (inc x) (+ x 1))
(define (id x) (x))

(sum (lambda (x) (if (odd? x) x 0)) 1 inc 100)

; or if you are sure a is odd and not even

(sum id 1 (lambda (x) (+ x 2)) 100)


You cannot do that with your procedures.

Name: Anonymous 2008-02-11 5:33

>>23
Try it first, dumbass.

> (sum (lambda (x) x) 1 (lambda (x) (+ x 2)) 100)
2500
(sum (lambda (x) (if (odd? x) x 0)) 1 (lambda (x) (+ x 1)) 100)
2500

Name: Anonymous 2008-02-11 9:20

>>21
YHBT again.

Name: Anonymous 2008-02-11 10:17

>>25
>>15 and >>21 are different people, dimwit.

Name: Anonymous 2008-02-11 10:31

read SICP

Name: Anonymous 2008-02-11 11:01

ITT: people who have actually read sicp

Name: Anonymous 2008-02-12 5:58

>>19
OP here, awesome!
I saw the more general case (ie the similarities sum and product have)
I did not know (lambda foo foo) is a list, heh.

Name: Anonymous 2008-02-12 6:37

OP again, exercise 1.42 & 1.43
Here is what I have

; Ex 1.42
; composes an arbitrary number of procedures

(define (compose . l)
  (lambda (x)
    (define (compose-iter l x)
       (if (null? l) x
           (compose-iter (cdr l) ((car l) x))))
     (compose-iter (reverse l) x)))


Works nicely.

As for exercise 1.43..


; Does scheme have such function? there is a make-string but I cannot find equivalent for lists

(define (make-list x n)
  (define (iter l n)
    (if (= n 0) l
        (iter (cons x l) (- n 1))))
  (iter '() n))

(define (repeated f n)
  (apply compose (make-list f n)))


What do you think?

Name: Anonymous 2008-02-12 7:42

>>30
in arc you just use : as an infix RCGAOENTHSAOENDTSHAOEUD ARC LOLOLOL

Name: Anonymous 2008-02-12 7:50

Could someone do it in Arc? I want to see how it looks like.

Name: Anonymous 2008-02-12 7:50

I could do it in Haskell.

Name: Anonymous 2008-02-12 8:02

>>33
...

Name: Anonymous 2008-02-12 8:16

>>34
(.).(.)

Name: Anonymous 2008-02-12 8:22

>>35
This might surprise you, but the does were Haskell compose operators, not the silence of pity.

Name: Anonymous 2008-02-12 8:22

s/does/full stops

Name: Anonymous 2008-02-12 8:34

>>36
Yeah, I kind of expected that... but you can't use the dots like that.

Name: Anonymous 2008-02-12 17:07

>>38
Yes you can:
Prelude> :t (.).(.)
(.).(.) :: (b -> c) -> (a -> a1 -> b) -> a -> a1 -> c


Name: Anonymous 2008-02-13 6:19

>>39
I was referring to ``...'' -- it might be possible to make a new function that's called (...) though.

Name: Anonymous 2010-12-21 4:31

Name: Anonymous 2011-02-03 3:28

Name: Anonymous 2013-03-18 18:54

Is it fine if I do all the SICP exercises in x86 ASM?

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