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: 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.

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