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

Pages: 1-4041-

First CLISP functions of a babby

Name: EXPERT BABBY 2011-01-22 16:41

Hey /prague/,
what do you think of my first functions? Do you think i am corrupted by the procedural paradigm already?


(defun factorial (a)
    "Calcula el factorial de a"
    (if (< a 1)
        1
        (* a (factorial (- a 1)))))

(defun get-n-elements (a n)
    "Crea una lista con los n primeros elementos de a,
    si a tiene menos de n elementos, rellena los elementos
    restantes con NIL"
    (cond
        ((<= n 0) nil)
        (T (append (list (car a)) (get-n-elements (rest a) (- n 1))))))
       

(defun get-n-to-m-elements (a n m)
    "Crea una lista con los elementos entre n y m de a"
    (cond
        ((not a) nil)
        ((> n m) (get-n-to-m-elements a m n))
        (T (reverse (get-n-elements (reverse (get-n-elements a m)) (+ (- m n) 1))))))

Name: EXPERT SCHEMER 2011-01-22 16:46

>>1
WHAT THE FUCK AM I READING.
YOUR FACTORIAL IS NOT TAILCALLOPTIMIZED!

I rewrote it for you:
(defun fact-iter (x c r) (if (= c x) r (fact-iter x (* c r))))
(defun fact (x) (fact-iter x 1 1))


Forgive my lack of CL skills.

Name: Anonymous 2011-01-22 16:52

>>2
I know, but since Lisp seems designed to think recursively I just did it. It's faster than I first thought, though.

Name: Anonymous 2011-01-22 16:54

>>1
WHAT THE FUCK AM I READING
YOUR GET-N-ELEMENTS IS NOT TAILCALLOPTIMIZED!

I rewrote it for you:
(defun gne-iter (x n r)
  (if (= n 0) (reverse r) (gne-iter (cdr x) (- n 1) (cons (car x) r))))
(defun get-n-elements (x n)
  (gne-iter x n null))


Forgive my lack of CL skills.

Name: Anonymous 2011-01-22 16:59

>>3
but since Lisp seems designed to think recursively I just did it.
Tail recursion is recursion.
Naive recursion is bad, you should avoid it when possible. Most CL implementations do TCO, and it has the loop macro anyway.

I suggest you to learn tail-call recursion first, and of course reading SICP.

Enjoy your Lisping.

Name: Anonymous 2011-01-22 17:05

>>5
Also, you can prepend an object to a list with (cons elem list), try to avoid (reverse (fun (reverse and (append (list.

Schemers usually build the list at the reverse, then reverse it in the end:
(defun some-loop (list count result)
   (if (stop-condition) (reverse result)
       (some-loop (cdr list) (+ count 1) (cons (car list) result))))

Name: Anonymous 2011-01-22 17:13

>>6,5
Wow, thank you /prog/
I'll try that.

Name: Anonymous 2011-01-22 17:15

HOW IS BABBY FORMED IN THE ART OF COMPUTER WIZARDRY??

Name: Anonymous 2011-01-22 17:34

>>2
YHBTCO

Name: Anonymous 2011-01-22 18:22

Name: Anonymous 2011-01-22 18:35

>>10
erlang
Does it even have GUARATEED TCO?

Name: Anonymous 2011-01-22 19:01

>>1
Why would you ever learn CLISP over Racket?
Racket is clearly superior in every aspect.

Name: Doctor Racket 2011-01-22 19:08

>>12
I agree, fellow Racketeer.

Name: Anonymous 2011-01-22 20:08

>>12
Not saying Racket is bad, however Common Lisp is pretty powerful if you know it well, and personally I prefer CL to Scheme. It's a matter of personal taste.

>>1
Here's some ENTERPRISE CL implementation of your code:

(defun range (start-or-count &optional (end (1- start-or-count) end-present-p)
        (by 1) &aux (start (if end-present-p start-or-count 0))) 
  (loop for i from start to end by by collect i))

(defun fact (n)
  (reduce #'* (range 1 n) :initial-value 1))

;; or more imperative (and faster)
(defun fact (n)
  (loop for i from 1 to n
    for result = 1 then (* result i)
    finally (return result)))

;; helper function which extends the sequence to length n if needed (because your code does that in get-n-elements)
(defun ensure-length (sequence n)
  (if (< (length sequence) n)
      (replace (make-sequence (class-of sequence) n :initial-element nil)
           sequence)
      sequence))

;; same as get-n-elements, but works on all kinds of sequences (lists, arrays, ...). In general, it would be best to just use subseq
(defun take (sequence n) 
  (subseq (ensure-length sequence n) 0 n))

;; again, use subseq instead, however to retain the same functionality as your code, I'm ensuring length is as expected (nil's will be added as needed)
(defun get-n-to-m-elements (sequence start end)
  (subseq (ensure-length sequence end) (1- start) end))

Name: Doctor Racket 2011-01-22 20:13

>>14
Your code made me cum.

Name: Anonymous 2011-01-22 20:19

>>14
does THIS:

(defun fact (n)
  (reduce #'* (range 1 n) :initial-value 1))

use STREAMS?

Name: Anonymous 2011-01-22 20:23

>>14
Well, I think comparing Common Lisp to Scheme is prima facie evidence of ill will, even if Common Lisp wins.  It is somewhat like a supposed compliment like "man, you are even smarter than George W. Bush".

Name: Anonymous 2011-01-22 21:07

>>16
No, but if you want lazy support, there are plenty of libraries which provide it. Lisps tend not to be lazy by default like ML or Haskell, but support for lazyness is usually provided in libraries (it's not hard to implement it by yourself either).

Name: Anonymous 2011-01-22 21:09

Well, I think posting quotes from deceased internet cranks is prima facie evidence of total ignorance of the topic being discussed, even if a bunch of spineless fanboys mindlessly jerk you off for submitting it. It is somewhat like a supposed contribution like "man, I don't have anything useful to show that's relevant to what you're talking about, but here's what some bitter jackass who used big words had to say about it."

Name: Anonymous 2011-01-22 21:29

>>18
(define-syntax delay (syntax-rules () ((~ e) (lambda () e)))) (define (force x) (x))

Name: Anonymous 2011-01-22 21:30

>>19
You sound a bit bitter yourself. You should work on your snark, though; it's not quite up to internet standards, though there is potential.

Name: Professional Autist 2011-01-22 21:45

>>21

While I appreciate your insightful analysis, you should note that the small amount of time I spent composing my message was used on making my remark within the format provided by the post I was mocking. I suppose you could say I was limited by the medium I was working with, and while I'm sure there was some potential for optimization, I really couldn't be bothered to change my phrasing to pack a maximum amount of "snark" into my post.

Name: Anonymous 2011-01-22 22:14

>>22
I can certainly agree with that; it's just that while I respect your desire to experiment as an aurtist, I think in retrospect, your choice of following the format may not have been appropriate for the message you wished to convey. More customization would have been required for the full payoff.

Name: Anonymous 2011-01-22 22:36

>>19
go fuck yourself nigger

Name: Professional Autist 2011-01-22 22:42

>>23

By imitating the post I was mocking, I created meaning just as valuable as anything offered by the words themselves (it shows total disrespect for the person being quoted, and some contempt for the person quoting him). It's sort of like the explanation people give for poetry - by itself, it would just seem like a very pretentious paragraph, but if you add a certain structure it not only becomes art, but allows for extra meaning to bleed through.

In this light, I think it is clear that my posting style was more than appropriate, since my intention was precisely to make my thesis (namely, that Erik Naggum was a self-important blowhard who is mostly quoted by uninformed people looking to idolize someone they deem as competent) well understood on multiple fronts; the mockery of Erik's quote only enhanced my message.

Now, I do wish I could have conveyed how hilariously awful it is that people have flocked to mindlessly to Naggum and held him up as some sort of idol, since he ranted about this exact behavior multiple times. However, attempting to make the message too dense would have destroyed the structure and made it seem sloppy. Thus, I have no regrets, and I am entirely content with the way things came out.

Perhaps if we keep this up long enough, we will fully embrace the spirit of Naggum and tie this whole discussion to a theory about Western politics or Epistemology!

Name: Professional Autist 2011-01-22 22:45

>>24

I'm sure Erik would be very proud of your eloquent retort if he were alive today!

Name: Anonymous 2011-01-22 22:56

>>26
you fucking faggot aren't worth of a proper response. now go eat shit and die

Name: Professional Autist 2011-01-22 23:04

>>27

I'm also apparently not worthy of a response that uses something resembling proper grammar.

Name: Anonymous 2011-01-22 23:13

>>28
i'm glad you figured it out. yo'ure lucky I haven't brought Ive into this.

Name: Professional Autist 2011-01-22 23:21

>>29

Being an idiot and pretending it was on purpose after the fact does not make you clever. Please try harder next time.

):

Name: Anonymous 2011-01-23 0:08

>>30
it sure beats being an idiot all along *hint hint*

Name: Professional Autist 2011-01-23 0:33

>>31

This is really pathetic. I was hoping you'd find some way to be clever, but all you've done is follow the boring "I was just trolling all along, LOL" line of thought.

Bah, I expected too much.

Name: Anonymous 2011-01-23 0:51

>>32
I was just trolling all along, LOL

Name: Anonymous 2011-01-23 1:05

k

Name: Doctor Racket !oRACKeT.F2 2011-01-23 6:28

>>22,25,26,28,30,32
That's why I said you couldn't go out the psychiatrical hospital.
Let's come back there now.

Name: EXPERT OP 2011-01-23 7:24

So, for starters, what Lisp-in-a-package implementation you reccomend? I am triying with clisp, but sure there are better things around there.

Name: Anonymous 2011-01-23 7:38

>>36
CL:
Windows: LispWorks (but free-as-in-gratis, you don't get GNUFreedom)
Linux: SBCL, CLISP is good for ``scripting''.

Scheme:
Chicken, Racket, MIT/GNU Scheme for SICP.

You may want to use Emacs+SLIME (or the LispWorks IDE on Windows) to write your Lisp code.

Name: 14 2011-01-23 8:16

>>36
I'm using Emacs+SLIME+Paredit+Redshank+multiple implementations, on all OSes that I use (Windows, Linux, *BSD).
Implementations I have installed are: SBCL (all *nix, Windows, but without threading; native compilation; public domain license), ClozureCL (fully functionality almost everywhere; native compilation; LLGPL), CLISP (everywhere; interpreted and bytecode; GPL), Lisp-Works (mostly everywhere; native compilation and interpreted; commercial), Allegro CL(mostly everywhere; native compilation; commercial, source available for customers), Corman CL(Windows-only; commercial, with source), ECL(everywhere, compiles to C and compiles to bytecode (thus native and interpreted, but relies on your C compiler);LLGPL). There's also some other ones like ABCL (for compiling to Java), and some new implementations like XCL. I've also tried Symbolics' OpenGenera, but it's not that stable on the PC. I mostly just use SBCL and ClozureCL for most of my developing, switching between them from time to time - actually testing my code in multiple implementations is easy in Emacs, as long as you have everything set up properly.

If you don't like Emacs, use LispWorks or Allegro CL as they have an IDE, however I think learning Emacs and SLIME will lead to increased productivity in the long run.

Name: Anonymous 2011-01-23 8:27

>>37
Racket works fine for SICP too. Even for the picture language chapter.

Name: Anonymous 2011-01-23 8:44

What the fuck is a BABBY?

Name: Anonymous 2011-01-23 10:01

>>37
After the 2.0 release happens (or is that, if it happens?), I think Guile should be added to that list.

Name: Anonymous 2011-01-23 11:42

>>40
Back to /b/ please (no, seriously, they'll tell you)

Name: Anonymous 2013-01-19 23:26

/prog/ will be spammed continuously until further notice. we apologize for any inconvenience this may cause.

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