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

The Little Schemer

Name: Anonymous 2009-07-19 23:23

Hi, I am working through the little schemer. I do not have the math skills to work through SICP.

I have the following code:


(define (member? a lat)
  (lambda (a lat)
    (cond
      ((null? lat) #f)
      (else (or (eq? (car lat) a)
                (member? a (cdr lat)))))))



When I run this say like:
(member? 3 (cons 3 '(9 8 7 6 13 3)))

I get the following:
#<procedure:...my-scheme-log.ss:137:2>
rather than a list without the first instance of 3

Can you help me?

Name: Scheme Noobie 2009-07-21 1:12

I just solved this challenge. Editing rember to remove all atom members that match mem.


;;mrember - removes multiple instances of an atom i.e. Multi Remove member
(define mrember
  (lambda (mem lat)
    (cond
      ((null? lat) '())
      ((eq? (car lat) mem) (mrember mem (cdr lat)))
      (else (cons (car lat) (mrember mem (cdr lat)))))))


>>40
Yeah, I have not gotten to IF yet but the writer said something about (cond) being as you described it. (cond) reminds me of case/switch style


I am using Dr. Scheme and I have it set for 5R5 scheme or r5r, I cannot remember. If I do not have it on 5R5 I cannot use lambda. Lambda is later in the book in a chapter called "Lambda the Ultimate".

The functions are named as such because the author says:

"Write the function x that does y." and then I write it according to his directions. I will definitely keep what you said in mind though. Do you mean like instead of substr
I should use remove-substring-from-list?

Thanks.

Name: Anonymous 2009-07-21 1:25

Do you mean like instead of substr I should use remove-substring-from-list?
You know, if the book you're using uses short names, stick with that style unless you find one you prefer. Don't listen to me about something so pointless.

Dr Scheme is good. You can use ctrl+\ instead of typing lambda.

Name: Scheme Noobie 2009-07-21 1:42

Here are the challenges from Chapter 3 (some repeated functions)

[/code]
;;mrember - removes multiple instances of an atom
(define mrember
  (lambda (mem lat)
    (cond
      ((null? lat) '())
      ((eq? (car lat) mem) (mrember mem (cdr lat)))
      (else (cons (car lat) (mrember mem (cdr lat)))))))
     
;;minsertR - multiple insertions to the right of the specified atom that occurs N times in a list
(define minsertR
  (lambda (new old lat)
    (cond
      ((null? lat) '())
      ((eq? (car lat) old) (cons old (cons new (minsertR new old (cdr lat)))))
      (else (cons (car lat) (minsertR new old (cdr lat)))))))

;;minsertL - opposite of minsertR
(define minsertL
  (lambda (new old lat)
    (cond
      ((null? lat) '())
      ((eq? (car lat) old) (cons new (cons old (minsertL new old (cdr lat)))))
      (else (cons (car lat) (minsertL new old (cdr lat)))))))

;;msubstr - substitutes all instances of an atom in a list
(define msubstr
  (lambda (new old lat)
    (cond
      ((null? lat) '())
      ((eq? (car lat) old) (cons new (msubstr new old (cdr lat))))
      (else (cons (car lat) (msubstr new old (cdr lat)))))))
[/code]


>>42
Oh okay. Thanks for the tips btw. I appreciate it. I hope this thread will help other people in the future trying to learn scheme. The most important thing I have learned so far is to actually write out the flow of execution if I am having problems understand how the program runs. It has become easier the more times I do it.

I really like this language so far. I think I am going to work through both of the books in the series and then write some small applications. I have an idea for a music program but that is a long ways away.

Name: Scheme Noobie 2009-07-21 1:43

I got a nice bbcode fail in 43 lol :)

Name: Anonymous 2009-07-21 2:01

Rewriting operators and learning about how to manipulate numbers in scheme:

my version:
(define +o
  (lambda (n m)
      (cond
        ((zero? n) m)
        ((zero? m) n)
        (else (+o (sub1 n) (add1 m))))))


book version
(define +o
  (lambda (n m)
      (cond
        ((zero? m) n)
        (else (add1 (+o n (add1 m)))))))


Are there any issues that you guys can see with my version vs. the book? My version was the first thing that came to mind.

Name: Anonymous 2009-07-21 16:04

Those two functions do not evaluate similarly, so someone is wrong.
> (define +o
  (lambda (n m)
      (cond
        ((zero? n) m)
        ((zero? m) n)
        (else (+o (sub1 n) (add1 m))))))
(+o 2 3)
5
(define ++o
  (lambda (n m)
      (cond
        ((zero? m) n)
        (else (add1 (+o n (add1 m)))))))
(++o 2 3)
7


Something is messed up. Did you incorrectly transcribe the book version?

Name: Scheme Noobie 2009-07-21 20:37

>>46
>Something is messed up. Did you incorrectly transcribe the book version?

Yes lol.

        (else (add1 (+o n (add1 m)))))))
should be
        (else (add1 (+o n (sub1 m)))))))

where sub1 is a function that subtracts 1.

Name: Anonymous 2009-07-21 21:02

Are there any issues that you guys can see with my version vs. the book?
You are performing two tests for almost all cases, while the book is only performing one. In one case, where m is zero, yours performs better, but in all other cases the book solution is faster. This could be improved by checking for this one case at the beginning only, then looping inside, but you probably haven't dealt with named lets or internal defines and such so it's best to let it go for the moment.

On the other hand, your version is properly tail recursive, while the book's solution is not.

Yours is like
(+o 3 2)
=>(+o 2 3)
=>(+o 1 4)
=>(+o 0 5)
=>5


The book's is like
(+o 3 2)
=>(add1 (+o 2 3))
=>(add1 (add1 (+o 1 4)))
=>(add1 (add1 (add1 (+o 0 5))))
=>(add1 (add1 (add1 5)))

etc.

Name: Scheme Noobie. 2009-07-21 23:54

>>48

Ahh yes, I see. Since n is always being subtracted I only need to test n. That makes sense.

Name: Scheme Noobie. 2009-07-22 0:40

What does tail recursive mean? I read the wikipedia article but it is fucking boring and it assumes that the reader is an EXPERT PROGRAMMER

Here is my code for defining the other operators and an extra function that sums the members of a tup.



;; define the subtraction operator.
(define -o
  (lambda (n m)
    (cond
      ((zero? m) n)
      (else (-o (sub1 n) (sub1 m))))))

;; define addtup which finds the sum of a tup.
(define addtup
  (lambda (lat)
    (cond
      ((null? lat) 0)
      (else (+o (car lat) (addtup (cdr lat)))))))

;; define the * operator.
(define *o
  (lambda (n m)
    (cond
      ((zero? m) 0)
      (else (+o n (*o  n (sub1 m)))))))

Name: Scheme Noobie. 2009-07-22 0:58

More on tups


;; write tup+ such that it returns a list of the sums of each element in each tup i.e. (tup+ (1 2 3) (1 2 3)) -> (2 4 6)
(define tup+
  (lambda (tup1 tup2)
    (cond
      ((and (null? tup1) (null? tup2)) '())
      (else (cons (+ (car tup1) (car tup2)) (tup+ (cdr tup1) (cdr tup2)))))))

;; add-two-tups adds each element in each tup together. Not sure why you would need this when I just wrote addtup and you could add
;; all of the members from tup2 to tup one and get the same answer. Oh well
(define add-two-tups
  (lambda (tup1 tup2)
    (cond
      ((and (null? tup1) (null? tup2)) 0)
      (else (+ (car tup1) (car tup2) (add-two-tups (cdr tup1) (cdr tup2)))))))

Name: Scheme Noobie. 2009-07-22 1:58

I do not understand lazy evaluation. I am going to finish this whole book then see if I understand it. I feel pretty comfortable with recursion now and it only takes me a few minutes to solve the challenges in this chapter. The end of the chapter is tougher.


;; define anytup+ such that tups of any number of members can be added
(define anytup+
  (lambda (tup1 tup2)
    (cond
      ((null? tup1) tup2)
      ((null? tup2) tup1)
      (else (cons (+o (car tup1) (car tup2)) (anytup+ (cdr tup1) (cdr tup2)))))))

;; define the equality operator
(define o=
  (lambda (n m)
    (cond
      ((or (o< n m) (o> n m)) #f)
      (else #t))))

;; recursively
(define myeq
  (lambda (n m)
    (cond
      ((and (zero? n) (zero? m)) #t)
      (else (= (sub1 n) (sub1 m))))))

;; define the exponentiation operator
(define o^
  (lambda (n m)
    (cond
      ((zero? m) 1)
      ((zero? n) 0)
    (else (o* n (o^ n (sub1 m)))))))

Name: Anonymous 2009-07-22 9:49

>>50
When you finish a function, execution has to go back to whoever called it to continue the program. For something like recursion, this could build up a huge list of promises to get back to the calling function, e.g, o+ needs to return to o+ needs to return to o+ needs to return to o+ needs to return to REPL. But if all you're going to do is return to the REPL, then there's no point in keeping around this long chain of recursive calls. When your function is properly tail recursive, it is possible for the compiler to make this happen.

Contrast this to the book's solution. The functions have to return to their caller because the caller has more work to do (add1 in this case). Your solution meant there was nothing left for the function to do.

I do not understand lazy evaluation.
Scheme is generally eager in the sense that all arguments to functions are evaluated prior to the function call. So you type
(foo (+ 3 4) (* 12 12) (factorial 1000))
And scheme goes through the motions
(foo 12 144 ......)
But it may be that foo never actually needs the value of 1000!. Maybe it uses it only when the first argument is not prime or whatever. In that case we wasted all this time and memory calculating (factorial 1000). Lazy evaluation delays the calculation of this until it is needed. If it is never needed, it is never calculated.

Name: Anonymous 2009-07-22 9:50

>>53
(foo 12 144 ......)
Of course that should be 7 not 12. LOL.

Name: Anonymous 2009-07-22 17:26

>>50
Read SICP online at http://www.sicp-online.org/

Name: Scheme Noobie 2009-07-23 0:51

Time for the nightly Little Schemer update:



;; define the function length
(define my-length
  (lambda (lat)
    (cond
      ((null? lat) 0)
      (else (add1 (length (cdr lat)))))))

;; define the function pick such that (pick n lat) will pick the nth item in the list lat - my version
(define pick
  (lambda (n lat)
    (cond
      ((null? lat) '())
      ((eq? 1 n) (car lat))
      (else (pick (sub1 n) (cdr lat))))))

;;book version
(define pick1
  (lambda (n lat)
    (cond
      ((zero? (sub1 n)) (car lat))
      (else (pick (sub1 n) (cdr lat))))))

;; define rempick such that (rempick N lat) will remove the nth item
(define rempick
  (lambda (n lat)
    (cond
      ((zero? (sub1 n)) (cdr lat))
      (else (cons (car lat) (rempick (sub1 n) (cdr lat)))))))

;; define no-nums such that (no-nums '(bob 1 wilson 2 john 3)) will return (bob wilson john)
(define no-nums
  (lambda (lat)
    (cond
      ((null? lat) '())
      ((number? (car lat)) (no-nums (cdr lat)))
      (else (cons (car lat) (no-nums (cdr lat)))))))

;; define all-nums such that (all-nums '(bob 1 wilson 2 john 3)) returns (1 2 3)
(define all-nums
  (lambda (lat)
    (cond
      ((null? lat) '())
      ((number? (car lat)) (cons (car lat) (all-nums (cdr lat))))
      (else (all-nums (cdr lat))))))

;; define eqan? such that (and (eqan? 1 1) (eqan? 'a 'a)) -> #t
(define eqan?
  (lambda (a b)
    (cond
      ((or (number? a) (number? b)) (= a b))
      ((eq? a b) #t)
      (else #f))))


Hi, chapter 4.

Also:

Is it true that ((null? lat) '()) is unneeded because we will never run into the null list? I think that is why my version
is stupid. I believe that my version (pick) includes a test for the null list when it is not needed. Whereas (pick1) stops at the test (zero? (sub1 n)) since we assume that the user will never pick an out of bound value.


(define pick
  (lambda (n lat)
    (cond
      ((null? lat) '())
      ((eq? 1 n) (car lat))
      (else (pick (sub1 n) (cdr lat))))))

;;book version
(define pick1
  (lambda (n lat)
    (cond
      ((zero? (sub1 n)) (car lat))
      (else (pick (sub1 n) (cdr lat))))))

Name: Scheme Noobie 2009-07-23 2:45

I do not understand how this works:


;; define occur such that (occur 1 '(1 2 3 4 1 1 1 5)) -> 4
(define occur
  (lambda (n lat)
    (cond
      ((null? lat) 0)
      ((eqan? (car lat) n) (add1 (occur n (cdr lat))))
      (else (occur n (cdr lat))))))


How can I use (add1) in the way mentioned above when it is defined as:


(define add1
   (lambda (n)
     (+ 1 n)))

Name: Scheme Noobie 2009-07-23 2:57

>>57
Actually, I think I see it.

(add1) is applied during the recursion in the comparison at (eqan? (car lat) n) (etc...)

Then we have:

(occur 1 '(1 1 1 b 3))
Evaluates to: (add1 (add1 (add1 0)
And finally evaluates to: 3

Fucking (scheme), do you want a lambda? Do you want a recursion? pig disgusting.

Name: =+=*=F=R=O=Z=E=N==V=O=I=D=*=+= !frozEn/KIg 2009-07-23 4:38

Scheme Noobie, get a free C compiler instead of wasting time with Scheme( recc. http://en.wikipedia.org/wiki/Tiny_C_Compiler
You are far likely to understand C then any functional language.



_____________________________________________
http://xs135.xs.to/xs135/09042/av922.jpg
A free press is not a privilege but an organic necessity in a great society. Without criticism and reliable and intelligent reporting, the government cannot govern. For there is no adequate way in which it can keep itself informed about what the people of the country are thinking and doing and wanting.

Name: =+=*=F=R=O=Z=E=N==C=H=E=F=*=+= 2009-07-23 4:38

Scheme-a Nuubeee-a, get a free-a C cumpeeler insteed ooff vesteeng teeme-a veet Scheme-a( recc. Bork Bork Bork! http://ee.veekipedia.oorg/veeki/Teeny_C_Cumpeeler
Yuoo ere-a fer leekely tu understund C zeen uny fooncshunel lungooege-a. Bork Bork Bork!



_____________________________________________
http://xs141.xs.to/xs141/09303/av992393.jpg
A free-a press is nut a preefilege-a boot un oorguneec necesseety in a greet suceeety. Bork Bork Bork! Veethuoot creeticism und releeeble-a und intelleegent repurteeng, zee gufernment cunnut gufern. Bork Bork Bork! Fur zeere-a is nu edeqooete-a vey in vheech it cun keep itselff inffurmed ebuoot vhet zee peuple-a ooff zee cuoontry ere-a theenking und dueeng und vunteeng.

Name: Anonymous 2009-07-23 9:17

>>58
I think you're doing good. Don't burn yourself out.

Name: Anonymous 2009-07-23 10:55

>>58
At this point I highly recommend downloading and watching at least the first four SICP videos. I predict a conceptual breakthrough. Just watch them. Don't code along with them, don't practice shit, just watch them.

Name: Anonymous 2009-07-23 11:13

>>59
Cool story bro

Name: Scheme Noobie 2009-07-23 11:42

>>59 must be Frozen Void. I cannot see it because I fixed prog with the greasemonkey script (and edited it to block frozenchef too)

FV I have installed the greasemonkey script that removes your posts.  Let me guess, his post was some troll shit about how useless functional languages are and how I should program everything in C? How do I know this? Well, because that is all you post, fucking lame troll shit that isn't even funny. Your code is unreadable and it is written in C.
I like scheme so far, a hell of a lot better than I like C.

No thanks. I have looked at C code and it is a fucking joke.

Just because this is my first language does not mean I am an idiot or have no experience with C. I bought a C book (K&R) about two years ago and after I learned pointers I realized that C is fucking stupid. I have no interest in creating operating systems or dealing with the huge amount of bugs that the coder must guard against when working with C. I have no interest in writing programs in "high level assembler". I have no interest in managing an array of chars to create a string when almost all modern languages will take care of that for me.
I have no interest in that stupid void * variableReturnType( args) bullshit. I have no interest in managing memory when 20983450293842 other languages will do it for me. C might teach you about the history of programming and that is great. But I have learned enough to know how the old programmers suffered while dealing with that trainwreck of a language.

I also have no interest in your shit posts FV. You are a troll.
Your "anti functional programming" posts only show that your career and life are limited by your bias and unwillingness to investigate different avenues. I have programmed in C out of the K&R book. It fucking sucks. Now everyone please install this:

http://userscripts.org/scripts/review/52726

Also, tell me if I was right, his shit post in >>59 was some anti fp troll right?


yeah yeah, IHBT

The rest of you guys, thanks a lot for your tips and help. I am really enjoying scheme. It is fun to think about these simple problems in scheme and develop these very small but very powerful ENTERPRISE SOLUTIONS
that would take three pages of text to create in java (god damn long syntax). You guys are cool dudes. Thanks a lot.

Name: Scheme Noobie 2009-07-23 11:47

>>62
Okay I will watch them.

Name: Anonymous 2009-07-23 11:49

>>64 may very well be the first case of someone being trolled without actually seeing the post.

Scheme Noobie, get a free C compiler instead of wasting time with Scheme( recc. http://en.wikipedia.org/wiki/Tiny_C_Compiler
You are far likely to understand C then any functional language.

Name: Scheme Noobie 2009-07-23 11:53

>>66
lol, I want to keep this thread so I am not going to post more but that is funny hahah.

I am going to go watch the sicp videos now.

Name: Scheme Noobie 2009-07-23 11:55

>>67
God my English fails.

I want to keep this thread. So I will not post anymore off topic shit.

Name: Anonymous 2009-07-23 12:07

>>62
Do you mean videos 1a - 4b or just videos 1a - 2b?

Name: Anonymous 2009-07-23 14:05

>>64
Bravo boy, your now one of us.

Name: Scheme Noobie 2009-07-23 16:47

I just finished watching the first video. It is much simpler then I remember it being. Maybe I watched a different video. I thought that sicp was math intensive. I like the square root example. I am going to watch 1b, 2a and 2b after I play some music.

Name: Anonymous 2009-07-23 16:54

>>71
I'm not sure I would call it math-intensive, but they do tend to use numerical examples.

Name: Anonymous 2009-07-23 16:56

>>71
You watched the right video. SICP might be somewhat math-intensive, but everything is explained as you go along, so there shouldn't be a problem. The most challenging math-related topic I remember seeing in SICP are integrals (or perhaps derivatives; anyway, the programming content is far more complicated than the math content). You are a good man. You also made me to watch the videos again *grabs SICP*.

Name: Scheme Noobie 2009-07-24 1:50

For anyone who is doubtful of the value of SICP.

If you are a new programmer that wants a short course on analysis of programs in a very succinct and user friendly manner I recommend that you watch the videos. This is quite possibly the most awesome scientific video I have ever watched. It is definitely worth it.

http://www.archive.org/details/mit_ocw_sicp

Name: Anonymous 2009-07-24 19:13

>>71
As >>72 says, the examples are somewhat on the numerical side, but that's not the reason they're so great (and that's also why I recommended simply watching them, rather than trying to code along with them).

I struggled with scheme for a bit and actually gave up. Then I watched the videos, and when one of the instructors (I think it was THE SUSSMAN) basically created car/cdr/cons out of thin air I had an epiphany, and immediately started coding in scheme like I'd known it my whole life. (Modulo macros.)

Name: Anonymous 2009-07-24 19:17

>>75
And then you get to lecture 7a, you realize that His Sussmanness is a wizard and that you will need to lock yourself away in an Ivory Tower for the rest of your life, pouring over his arcane lore.

Name: Anonymous 2009-07-24 20:20

>>76
pouring what?

Name: Anonymous 2009-07-24 20:27

>>77
hot cum, or you could imagine I wrote "poring" as I had meant to.

Name: Anonymous 2009-07-24 20:28

Name: Anonymous 2009-07-24 20:50

>>78
To do this I would have to admit side-effects and it is outside the scope of the functional style.

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