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

Pages: 1-4041-8081-

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: In before DONT HELP HIM 2009-07-19 23:56

You're a little confused on the function declaration syntax. You can do this:
(define (foo x)
  bar)

Or this:
(define foo
  (lambda (x)
    bar))

Those mean the same thing. But if you do this:
(define (foo x)
  (lambda anything))

...That defines foo as a function that returns another function.

The first example is syntactic sugar for the second, by the way.

Name: Anonymous 2009-07-20 0:05

>>2
Thank you. I see what you are saying. In one expression, lambda is the argument to foo where as lambda anything is just a standalone lambda expression (i.e. anonymous function) like python's lambda.

So when I ran this I was seeing the result of the return of the (lambda ...) portion rather than the top level definition.


Hey, this is pretty cool. I like scheme. This is my first functional language and I like it so far.

Thanks >>2! I appreciate the help.

Name: Anonymous 2009-07-20 0:41

This language makes me feel good. I feel like I am really understand what is going on with my programs. Even though they are small. I am understanding program flow for the first time...

The Little Schemer is a good book...

Name: Anonymous 2009-07-20 2:56

>>2

Helpful replies, on MY /prog/???

Name: Anonymous 2009-07-20 3:14

>>5
DON'T BE HELPED BY HIM!!!

Name: Anonymous 2009-07-20 12:28

Hi guys. It is me again, >>3.
Thanks.

Name: Anonymous 2009-07-20 12:38

>>2
Though it runs against some styles, I prefer explicit lambdas under defines for this reason.
(define square
  (λ(x)
    (* x x)))

This is straightforward: "define 'square' to be a function that..." This is the style used extensively in The Scheme Programming Language. SICP seems to prefer the other format. I guess it is a matter of taste.

Name: =+=*=F=R=O=Z=E=N==V=O=I=D=*=+= !frozEn/KIg 2009-07-20 12:54

all of these are much more "straightforward" >>8 :
function square(x) x*x;
int square(int num){return num*num;}
square x=x*x


_______________________________________________
http://xs135.xs.to/xs135/09042/av922.jpg
Behind every failure is an opportunity somebody wishes they had missed.

Name: Anonymous 2009-07-20 13:22

>>8
I like (define (f x) (* x x)) simply because it says that if you write (f x), you get (* x x). It's more like Haskell's f x = x * x; you wouldn't write f = \x -> x * x (or maybe you would).

Name: Anonymous 2009-07-20 13:35

>>10
Interesting interpretation, thanks. It's the first time I've ever heard something in favor of it (besides "the other way is stupid"). It makes sense.

Name: =+=*=F=R=O=Z=E=N==V=O=I=D=*=+= !frozEn/KIg 2009-07-20 13:44

>>11 JavaScript analogue
square=function(num) num*num;
function square(num) num*num;

_____________________________________
http://xs135.xs.to/xs135/09042/av922.jpg
I think, and my thoughts cross the barrier into the synapses of the machine - just as the good doctor intended. But what I cannot shake, and what hints at things to come, is that thoughts cross back. In my dreams the sensibility of the machine invades the periphery of my consciousness. Dark. Rigid. Cold. Alien. Evolution is at work here, but just what is evolving remains to be seen.

Name: 11 2009-07-20 13:50

>>10
Also, this explains well why SICP seems to adopt this style, because it places quite a bit of emphasis on the substitution model of mental interpretation.

Name: Anonymous 2009-07-20 14:17

>>10
I would write
f = product . replicate 2

Name: =+=*=F=R=O=Z=E=N==V=O=I=D=*=+= !frozEn/KIg 2009-07-20 14:34

>>14 JavaScript analogue:
function square(){var c=1;for(a=0;a<2;a++){c*=arguments[0];};return c}



__________________________________________
http://xs135.xs.to/xs135/09042/av922.jpg
No great idea in its beginning can ever be within the law. How can it be within the law? The law is stationary. The law is fixed. The law is a chariot wheel which binds us all regardless of conditions or place or time.

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

as inline JS expression:
square=function() arguments[0]*arguments[0]

_________________________________
http://xs135.xs.to/xs135/09042/av922.jpg
Turn on, Tune in, Drop out.

Name: =+=*=F=R=O=Z=E=N==V=O=I=D=*=+= !frozEn/KIg 2009-07-20 14:52

I think this is what Haskell does in >>14 ..if i'm correct
function mularr(c){v=1;for(i in c){v*=c[i]};return v}
function duplicate(x,count){c=[];for(a=0;a<count;a++){c[a]=x;};return c}
square=function() mularr(duplicate(arguments[0],2))



_____________________________________
http://xs135.xs.to/xs135/09042/av922.jpg
The news and the truth are not the same thing.

Name: =+=*=F=R=O=Z=E=N==V=O=I=D=*=+= !frozEn/KIg 2009-07-20 16:05

#include <stdio.h>
#include <stdlib.h>
//haskell style square function: it took far longer to debug then to write.

int multiplyArray(int *arr,int arrsize){
int res=arr[0];int x=0;
while(++x<arrsize){res*=arr[x];};return res;}

int* duplicateInts(int num,int count){
int *storeInts=calloc(count,sizeof(int));int loop;
for(loop=0;loop<count;loop++){storeInts[loop]=num;};return storeInts;}

void main(int argc,char **argv){printf("%i",multiplyArray(duplicateInts(atoi(argv[1])?atoi(argv[1]):0,2),2));}




___________________________________
http://xs135.xs.to/xs135/09042/av922.jpg
Nature is busy creating absolutely unique individuals, whereas culture has invented a single mold to which all must conform. It is grotesque.

Name: Anonymous 2009-07-20 16:08

>>18
Got a memory leak in there.

Name: =+=*=F=R=O=Z=E=N==V=O=I=D=*=+= !frozEn/KIg 2009-07-20 16:19

>>18 ..but memory are not important according to:
http://dis.4chan.org/read/prog/1247978789/114
http://dis.4chan.org/read/prog/1247978789/130
http://dis.4chan.org/read/prog/1247978789/156


________________________________________________
http://xs135.xs.to/xs135/09042/av922.jpg
orbis terrarum delenda est

Name: Anonymous 2009-07-20 16:23

>>20 Expert Quoting there FV ;)

Name: =+=*=F=R=O=Z=E=N==V=O=I=D=*=+= !frozEn/KIg 2009-07-20 16:32

#include <stdio.h>
#include <stdlib.h>
//this is how i envision haskell power (x^y) function to work with something like func=product.replicate x;

int multiplyArray(int *arr,int arrsize){
int res=arr[0];int x=0;
while(++x<arrsize){res*=arr[x];};return res;}

int* duplicateInts(int num,int count){
int *storeInts=calloc(count,sizeof(int));int loop;
for(loop=0;loop<count;loop++){storeInts[loop]=num;};return storeInts;}

void main(int argc,char **argv){printf("%i",multiplyArray(duplicateInts(atoi(argv[1])?atoi(argv[1]):0,atoi(argv[2])?atoi(argv[2]):1),atoi(argv[2])?atoi(argv[2]):1));}




______________________________
http://xs135.xs.to/xs135/09042/av922.jpg
The knowable world is incomplete if seen from any one point of view, incoherent if seen from all points of view at once, and empty if seen from nowhere in particular

Name: Anonymous 2009-07-20 17:09

jesus christ frozenvoid how the fuck is anyone meant to read your code

Name: =+=*=F=R=O=Z=E=N==V=O=I=D=*=+= !frozEn/KIg 2009-07-20 17:12

#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
//this is how i envision haskell power (x^y) function to work with something like func=product.replicate x;
//Now with arbitrary precision(with GMP)

void multiplyArray(int *arr,int arrsize,mpz_t num){
int x=0;mpz_set_si(num,arr[0]);
while(++x<arrsize){mpz_mul_si(num,num,arr[x]);};}

int* duplicateInts(int num,int count){
int *storeInts=calloc(count,sizeof(int));int loop;
for(loop=0;loop<count;loop++){storeInts[loop]=num;};return storeInts;}

void main(int argc,char **argv){
mpz_t num;mpz_init(num);
multiplyArray(duplicateInts(atoi(argv[1])?atoi(argv[1]):0,atoi(argv[2])?atoi(argv[2]):1),atoi(argv[2])?atoi(argv[2]):1,num);
mpz_out_str(stdout,10,num);}




____________________________________
http://xs135.xs.to/xs135/09042/av922.jpg
I prefer to be true to myself, even at the hazard of incurring the ridicule of others, rather than to be false, and to incur my own abhorrence.

Name: Anonymous 2009-07-20 17:23

jesus christ frozenvoid how the fuck is anyone meant to read your code

Name: Anonymous 2009-07-20 18:46

>>1 Here.

Will thinking about how to solve problems with recursion get easier with time? Or should I read more stuff about functional programming?

Name: Anonymous 2009-07-20 19:01

>>24
void main, you're a fucking idiot, undefined behavior, et cetera.

Name: Anonymous 2009-07-20 19:12

>>27
☣ Please try to ignore troll posts! ☣

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

Name: Anonymous 2009-07-20 19:22

>>28
I could write this myself. Why don't you go fuck yourself

Name: Anonymous 2009-07-20 19:57

>>29
when did /prog/ turn into such an angry place ;_;

Name: Anonymous 2009-07-20 20:09

>>29
IS AN ENTERPRISE LEVEL FAGGOT

Name: Anonymous 2009-07-20 20:32

>>26
Much easier. Are you coming from another language or just starting with scheme?

Name: Anonymous 2009-07-20 20:45

>>32
This is my first language.

I finally figured the insertL shit out without cheating. I had to actually write out all of the steps and returns in the recursion lol.


;;write insertL
(define insertL
  (lambda (new old lat)
    (cond
      ((null? lat) '())
      (else
       (cond
         ((eq? (car lat) old) (cons new (cons old (cdr lat))))
         (else (cons (car lat) (insertL new old (cdr lat)))))))))


Calling:
(insertL 'fudge 'topping '(ice cream with topping for dessert))

Answer:
(ice cream with fudge topping for dessert)


And it works as expected. I am really enjoying this.

Name: Anonymous 2009-07-20 20:46

>>33
Why does the bbcode parser suck ass for scheme?

Name: Anonymous 2009-07-20 20:51

>>33 here.

I need to write substr now. BRB!

Name: Anonymous 2009-07-20 20:58

))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))

Name: Anonymous 2009-07-20 21:14

>>34
Take it up whoever made your browser

Name: Scheme Noobie 2009-07-21 0:44

Hi friends. I am going to use a name to keep track of my progress. I hope that you guys don't mind. I am not trolling.
I just want to keep track of this.


;;write substr, with single arg - not following instructions
(define substr
  (lambda (rem lat)
    (cond
      ((null? lat) '())
      ((eq? (car lat) rem) (cdr lat))
      (else
       (cons (car lat) (substr rem (cdr lat)))))))

;; following the instructions now lol
(define substr-right
  (lambda (new old lat)
    (cond
      ((null? lat) '())
      ((eq? (car lat) old) (cons new (cdr lat)))
      (else (cons (car lat) (substr-right new old (cdr lat)))))))

;;write substr2
(define substr2
  (lambda (new o1 o2 lat)
    (cond
      ((null? lat) '())
      ((eq? (car lat) o1) (cons new (cdr lat)))
      ((eq? (car lat) o2) (cons new (cdr lat)))
      (else
       (cons (car lat) (substr2 new o1 o2 (cdr lat)))))))

;;substr2 v2 - using or - review how to use or and else and stuff. There are so many parentheses lol.
(define substr2-v2
  (lambda (new o1 o2 lat)
    (cond
      ((null? lat) '())
      ((or (eq? (car lat) o1) (eq? (car lat) o2)) (cons new (cdr lat)))
      (else
       (cons (car lat) (substr2 new o1 o2 (cdr lat)))))))


These are all recursive functions and challenges from The Little Schemer. It is a great book and really allows me to learn scheme. I like it so far. I feel like I am finally understanding recursion and how to reason about programs.
This makes it more fun to program. It would have taken me more time to write these in python (not flaming, just my personal opinion) I know python as well as I know scheme. Not very well lol.

I am looking forward to learning more about the lambda item and more functions.


Hey guys, if I learn scheme can I learn lisp too? They look the same and I read that scheme is a dialect of lisp.

Thanks.

Name: Scheme Noobie 2009-07-21 0:55

I was sitting here thinking about how to do something in Scheme and I said out loud:

"Now return the coulder"

I lol'd

Name: Anonymous 2009-07-21 1:04

>>33
You don't need to break up the cond tests here, that's the purpose of cond over if. (Seems like you realize this in >>38 but I just want to be sure.)

Which implementation are you using?

BTW you already encountered the member method which returns the sublist from the first match. No need to rewrite it! (Unless that was an exercise.)

Finally, there's no need to be so cryptic with your names. I know it is hard to believe what with "car", "cdr", "cons", and such, but you should try to be a little more descriptive. In other languages, having to retype long names several times is a pain, but with closures and first class functions Scheme doesn't really fall into the problem of too much typing.

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.

Name: Anonymous 2009-07-24 21:35

>>80
( ≖‿≖)

Name: Anonymous 2009-07-25 4:44

>>80
Daddy, I am a side-effect?

Name: Anonymous 2009-07-25 5:41

PUBLIC SERVICE ANNOUNCEMENT:

20721 IS NOT BEING CRAPFLOODED

Name: Anonymous 2009-07-25 5:47

Be Safe! Use a Monad!

Name: Anonymous 2009-07-25 12:51

>>82
I almost lol'd.

Name: Scheme Noobie 2009-07-26 0:41

Wow, this is a great series. It really has opened my eyes to things I did not understand. I feel very comfortable with recursion now. I am going to watch 2b and return to writing scheme code. Thanks guys.

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

Working through The Little Schemer again after watching those videos. I am good with recursion on a flat list but nested lists are a challenge. I am going to keep programming in Scheme until I am good at it and maybe move to Lisp or Haskell. I like Scheme.

Name: Anonymous 2009-07-27 2:03

>>87
maybe move to Lisp
Common Lisp and Scheme are both forms of Lisp. Please don't call CL "Lisp" to differentiate it from Scheme. The original Lisp was more like Scheme than like CL anyway.

Name: Anonymous 2009-07-27 2:07

>>87
It's good to have a type system to keep you from getting lost in the depths of car and cdr

Name: Anonymous 2009-07-27 4:47

>>89
He will probably understand lists much better after finishing Little Schemer and SICP, and I don't think he'll get lost if he truly understands all the implications. Having a strong inferrence-based type system is nice to some degree, as it means safer and faster(at less cost to clarity than in lisp) programs, but it can also get in your way unnecesarily, but don't get me wrong, Haskell is a very good language with many strengths, although different from dynamic languages like Lisp.

I think it's a good idea to learn CL now that he knows Scheme, as it's a more practical language, not that Scheme isn't practical, but Scheme was designed with theorethical beauty and language cleanliness in mind, while Common Lisp was made with pratical issues in mind and writing larger real world programs, and thus is a much larger language. This doesn't mean you'll find all kinds of crap that gets placed in the Java or .NET libraries. CL's functions/macros, even though many in number, are meant to make general programming easier/faster/more efficient, not to give easy solutions to the trendy problems of today, however CL is a programmable programming language(in the sense that defining new operators(as functions or macros) is easy and encouraged), and surely solving modern problems isn't that hard, and there's many user-made libraries for those too.

Name: Anonymous 2009-07-27 15:12

>>88
The original Lisp was more like Scheme than like CL anyway.
Wat. It didn't even have lexical scoping or closures. And I'm pretty sure it didn't have unhygienic namespaces.

Name: Anonymous 2009-07-27 15:24

>>91
YHBT, >>88 is clearly a troll

Name: Anonymous 2009-07-27 16:27

>>92
I'm fucking your commonly

Name: Anonymous 2009-07-27 18:27

>>93
You're an idiot.

Name: Anonymous 2010-12-08 9:16

wat

Name: Anonymous 2010-12-08 9:16

wat

Name: Anonymous 2010-12-08 9:17

WAT?

Name: Anonymous 2010-12-08 9:18

WAT?

Name: Anonymous 2010-12-10 10:11

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