lainchan / λ

MIT Scheme for C-tards

vi-user · 2021-04-24 14:41
Was a C-tard for a long time, recently decided to start learning MIT Scheme and join the cult. You people on the wired have any tips or cool Scheme scripts/tricks I should know of?
Linda Lee · 2021-04-24 21:01
>>25595 There's a lisp general thread somewhere on lambda, look there and you may find answers that you seek from actual seasoned lisp and scheme programmers. We free schemers prefer "community" to "cult" thank you very much ;) >c-tard don't be rude on lain please, this isn't 4chan.
vi-user · 2021-04-24 21:28
>>25598 Dope! I found the thread, thanks. >don't be rude on lain please, this isn't 4chan. My bad.
Mario Rossi · 2021-04-26 00:54
>>25595 A nice time saver for the MIT Scheme system specifically; when you encounter an error and are dropped into the restart system, you can just hit control+g or control+c twice get back up to the top level. Also do take some time to learn the (debug)ger, I feel that it's among one of the best that I've played with, and its integration with the (edwin) is very good if you're into that. For more general Scheme hacks, don't forget the freedom Scheme offers you to fuarrrk around with things. It permits the redefinition of all primitives. Scheme is perhaps one of the few (only?) systems to give the programmer such freedom, not even Common Lisp would let you do this (while Common Lisp is bad for many reasons, this one I'll give them a pass on this one. Redefinition of primitives is prohibited to allow the compiler to preform very specific optimization tricks, while I consider this to be a poor decision, I don't believe it's an inherent flaw of the Common Lisp system). Let's say you don't like how applying cdr to an empty list throws an error instead of returning the empty list again (as it does in Common Lisp). Understandably this can be frustrating for some, and a dirty hack to fix this behavior follows: (define cdr (let ((old-cdr cdr)) (lambda (x) (if (eq? x '()) '() (old-cdr x))))) And if you'd like this hack to reach into the depths of the Scheme system itself (circumvent their little safety feature to keep you from fuarrrking the Scheme system up too bad if internal names collide with yours) simply: (link-variables system-global-environment 'cdr (the-environment) 'cdr) There exists a much better version of this hack, but that is left as an exercise for the reader ;) One more quick thing which I think lots of people will find handy, consider the following definition: (define-syntax define-macro (syntax-rules () ((define-macro (name . args) body ...) (define-syntax name (rsc-macro-transformer (let ((transformer (lambda args body ...))) (lambda (exp env) (apply transformer (cdr exp))))))))) Evaluate this and you can now define full power Common Lisp macros that, yes, break hygienics. You can find a lot more neat hacks around the web and on the old USENET postings. >>25598 Would you perhaps be happier with 'C-nile'?
Davis · 2021-04-26 01:57
>>25599 The lisp thread is probably the best thread on lambda. Scheme is my personal favourite lisp coming from Python, the absurd amount of flexibility has allowed me to do both very stupid and very cool things in the interpreter. Also, welcome to lainchan - have a great stay, lets all love lain! >>25612 >Would you perhaps be happier with 'C-nile'? namecalling in general is a great way to spill soykaf and ruin board culture. I understand the critique that some c programmers are stubborn and narrow-minded on their language choices and their defences of features (or lack thereof) in C, but namecalling is a terrible way to address those flaws in a manner that really benefits anyone. namecalling just makes people angry and sad. > scheme hacks this is why scheme is my favourite lisp.
vi-user · 2021-04-26 15:42
>>25612 The hacks look dope! The cdr redefinition is def something I'll look into. >Would you perhaps be happier with 'C-nile'? C-nile sounds better! It better reflects my overall attitude
Spooky · 2021-04-27 08:34
>>25612 >Evaluate this and you can now define full power Common Lisp macros that, yes, break hygienics. Can you explain how you would use this macro? It seems really interesting.
William Holtzer · 2021-04-28 17:00
>>25619 >Can you explain how you would use this macro? Just as you would in Common Lisp, using quasi-quoting. Here's a very basic example, lets translate Paul Grahams aif Common Lisp macro (defmacro aif (test then &optional else) `(let ((it ,test)) (if it ,then ,else))) to Scheme, this is easily done with define-macro. (define-macro (aif test then #!optional else) `(let ((it ,test) (if it ,then ,else))) And to demonstrate unhygienics, I translate a classic Common Lisp example (define-macro (my-unless condition body) `(if (not (user-defined-operator ,condition)) (begin ,@body))) (let ((user-defined-operator not)) (my-unless #t (pp "This should not be printed!"))) ;; but it is!
x<->y · 2021-04-28 20:38
>>25624 Oh that really looks a lot simpler than the whole Scheme DSL of syntax-rules. Thank you for your examples, I'll definitely check this out!
Cereal Killer · 2021-04-28 22:50
C programmer is a rust maymay. Lisps are older than C, it wouldn't even make sense.
John Rain · 2021-04-28 22:51
>>25627 Lol senile was filtered
vi-user · 2021-04-28 22:56
>>25628 lol C-nycial also works, also reflects the attitudes of C programmers better.
null · 2021-04-29 16:16
>>25627 >Lisps are older than C, it wouldn't even make sense. Lisp is still young, we haven't found its limits yet. Besides, C-nile is a reference to the C Programmers that think we're still in the 1970s using teletypes.
Faye Valentine · 2021-04-29 16:19
>>25626 They're simpler but much more low level. Scheme's macro system is much nicer in it's own ways. It's certainly still worth learning.
DEBUG-CONTENTS · 2021-10-14 05:00
no one will notice this post, but i am sad. ;(
push · 2021-10-14 11:27
>>27977 Well I did notice this post, since you didn't manage to sage correctly. I hope you get better soon, and don't remain sad for too long.
Apoc · 2021-10-14 23:22
>>27977 There's a betr thread for this in /hum/ you fuarrrking spammer
Incognito · 2021-12-19 14:54
>>29080 I've thought about it a bit more and I could potentially manually implement it using load and checking for changes on the source files, I guess the options are hashing and file modification times, with the second likely being the better option for performance, though lacking portability.
Chani · 2021-12-19 22:46
Does anyone have any idea about run time code reloading in MIT Scheme? I'd love to use it to write a game but I want editability to be at the forefront and I feel run time code reloading would be a great benefit. I'd prefer MIT Scheme over Common Lisp (which I know has it) due to its simplicity.
Spike Spiegel · 2021-12-20 01:01
>>29081 Can't you make an interface to inotify (or just use a pipeline to entr, which is more portable)?
Ender · 2021-12-22 20:37
>>29080 >>29081 All good lisps exhibit this property, and MIT Scheme is one of the best.
par22 · 2021-12-26 05:27
Why MIT Scheme specifically? There are so many other Scheme implementations. For example: Chez, Guile, etc.
Agent Smith · 2021-12-28 07:31
>>29179 I think it's just because of SICP. The foreword linking to the course materials tells the user how to acquire MIT scheme and so most people who learnt scheme through the book used MIT scheme. This then made it the arguably dominant scheme implementation, so it's what is used here (searching for 'scheme download' will return mit scheme first on google).
J.C. Denton · 2021-12-28 08:28
I still don't see a reason to use lisp, nor do I know any major software employing this language.
Unspecified · 2021-12-28 09:08
>>29207 >I still don't see a reason to use lisp Reasons why Lisp is good: - Powerful and interactive programming environment (REPL) and a powerful editor (Edwin or Gnu Emacs and Geiser/SLIME & rainbow-delimiters). - Scheme is small and simple language. Common Lisp is larger but manages to remain sane; you don't pay for what you don't use. - Good and mature standards and implementations (for example: Chez Scheme, MIT Scheme, Chicken, Guile & Racket & SBCL, CCL). - Lisp macros allow you to extend the language seamlessly. - Simple syntax (you use the same syntax for almost everything) and code is data, data are code. - Image-based development and deployment (like Smalltalk) Basically, you can save the current state of the "world" and load it later. It's a bit hard to explain, idk, just try it. - Common Lisp, Scheme and Racket support multiple styles (or paradigms) of programming. Have you read your SICP, yet? Install Guile and start: >https://mitpress.mit.edu/sites/default/files/sicp/index.html >https://www.youtube.com/watch?v=-J_xL4IGhJA&list=PLE18841CABEA24090&index=1 Or how about The Little Schemer? Also, read the documentation of the implementation you use (this is especially important if you use Scheme) For Common Lisp related things, see... >http://www.lispworks.com/documentation/HyperSpec/Front/ >https://lispcookbook.github.io/cl-cookbook/ >http://weitz.de/cl-recipes/ >Common Lisp: A Gentle Introduction to Symbolic Computation
byte · 2021-12-28 12:32
>>29207 Lisp teaches you how programming languages manage syntax and semantic in the same way C teaches you how programming languages manage resources. Nearly every feature in Lisp is implemented atop a small set of very simple primitives that are also available to the programmer. You can really get into the guts of the Lisp, extending the language with new features as you need them. Implementing a feature yourself gives you a much deeper understanding of how it works in other languages. Even if you don't use Lisp much beyond educational purposes, mastering it will make you a significantly better all-around programmer.
Anonymous · 2021-12-28 14:06
>>29210 Mate, Practical Common Lisp is the CL book to start with. Seibel even manages to show the strengths of CL through practical but simple examples, which isn't always easy to do (e.g. for the condition/restart system).
Tank · 2021-12-29 17:52
>>29207 crash Bandicoot was written in lisp >>29215 >extending the language I hear this a lot, but what does it actually mean, and how is it unique to lisp.
Apoc · 2021-12-29 18:15
>>29238 Well if you think about it, an if-then structure in code translates to a bunch of awkward-looking comparisons and jumps in assembly. If you had a language with less abstractions, call it BASIC, you'd be forced to write something very close to all those comparisons and jumps, say with stuff like 'GOTO 100'. In many other languages, such as C, python and java, the syntax is fixed by the designers and you can only use what's given. As your software becomes more complex and uses some specific data structures that require specific loopings and tests, you only have the syntax of the language available, you might need to repeat some lengthy constructs at different points. Consider for example doing matrix multiplication, and now in arbitrary dimensions because your physics engine uses them for different things at different points. With lisp, you can also extend the syntax of the language through a more sophisticated macro system so that you can do what language designers did when they abstracted all those jumps and tests into a straightforward descriptive 'if-then' construct. You might think that there are other ways to work around this, or that it is not strictly necessary. You might be right, but the fact that the language provides you with this ability is convenient and where much of the power of lisp resides. Not in the ability per se of masking complex pieces of code behind a descriptive name, but in giving you the power you should have on the first place, of designing the abstractions of the language the same way a high-level-language designer did for you when they decided what abstractions to provide for their language. There is a subtle difference between macros and functions. Books have been written about this.
Roy Batty · 2021-12-30 14:34
>>29239 >you can also extend the syntax of the language through a more sophisticated macro system now this is what confuses me, now technically you could program any language into any language and extend C into Lisp etc, but this is obviously not what we are talking about. Can you extend lisp from a language that uses ( brackets to one that uses [ via macros? or that has type checking etc? >You might think that there are other ways to work around this Ya, I was thinking regular functions. In C you could create an if function ( int if(x, y); ignoring illegal names of course.) This isn't changing the syntax technically, but allowing you to do ifs without the if{}'s I'm out of my depths with lisp things, and probably don't know definitions or something
Frank Garza · 2021-12-30 16:27
>>29239 >Consider for example doing matrix multiplication, and now in arbitrary dimensions because your physics engine uses them for different things at different points. Generally the libraries people actually use would simply hold differently-sized matrices in different data structures, and I'm pretty sure the LAPACK libraries and similar have highly optimized assembler routines.
Sipho Nkosi · 2021-12-30 18:36
>>29252 >Can you extend lisp from a language that uses ( brackets to one that uses [ via macros? Not with macros, because macros operate not at the level of characters but symbolic expressions, which are just standard Lisp data structures (numbers, symbols, lists, etc). Since Lisp syntax maps so directly to those data structures (for example, the expression (+ 1 2) is just a list containing 3 elements, the symbol + and numbers 1 and 2), the transformations feel very natural and are simple to reason about, as opposed to a language with a more elaborate AST representation. Common Lisp in specific allows you to do that with that is called a reader macro, which is executed by the reader and allows you to arbitrarily redefine character-level syntax for the language, but otherwise has little to do with what most people are talking about when they talk about macros. >or that has type checking [...]? Sure. Common Lisp itself has a few forms to declare types and whatnot, but honoring them is up to the implementation; nothing is stopping you from declaring your own forms that do type-checking on their own, however. >Ya, I was thinking regular functions. In C you could create an if function ( int if(x, y); ignoring illegal names of course.) This isn't changing the syntax technically, but allowing you to do ifs without the if{}'s That wouldn't work, because your if function has no ability to control evaluation. Think about what would happen if your y form had side-effects, or didn't terminate (think recursive functions). It is an interesting point to build upon however: Let's think of a hypothetical Lisp that has function definitions, labels, comparisons and a operator, cond-jmp, that jumps to a specified label if the 1st argument is true. Typical conditional code looks like this: (cond-jmp (= x 0) rest) ;; x is not 0 here rest ;; unconditional code here In such a language, you could try to define your if function like so: (defun if (x y) (cond-jmp x end) (return y) end) This works, as long as the form that evaluates to the value bound by y is pure and terminates. However, when writing some game code, you come across the following: ;; some logic (if (>= (health player) 0) (kill-player player)) ;; more logic kill-player is a function that doesn't return any useful result, but has the side-effect of killing the player you passed in as an argument. Since your if function has no ability to manipulate evaluation, the player is always killed, no matter it's health. In a language without macros, you would be forced to give up and repeat the idiom in the first example (or finding a way to defer the evaluation of the second form, perhaps by having if expect it to be a continuation that it can call rather than any arbitrary value, or quoting it and evaling; either case wouldn't be transparent to the user and come with it's own set of drawbacks). If we concede that our hypothetical Lisp has macros, however, one could define if as a macro like the following: (defmacro if (x y) (list 'progn (list 'cond-jmp (list 'not x) 'end) y 'end)) defmacro defines a macro, which is like a function that the compiler calls at compile time, and then replaces the code with the return value. list is just a constructor for lists (remember that Lisp code is formed primarily out of lists and atomic structures, like numbers and symbols), progn roughly forms a block of sequential code (think like a {} block in C) and ' is a operator that roughly means "do not evaluate the next expression"; for example, (+ 1 2) evaluates to 3, but '(+ 1 2) evaluates to the list (+ 1 2). Note that actual Lisps have things like quasiquotes, which makes writing "template-style" macros like this a tad less awkward. Back to the game code, when the compiler encounters your if form, it will call your macro with the list (>= (health player) 0) as the first argument and (kill-player player) as the second argument, and splice in the return value. The macroexpanded code will look like the following: ;; some logic (progn (cond-jmp (not (>= (health player) 0)) end) (kill-player player) end) ;; more logic Which is, aside for the progn, essentially the same as you would have written if you didn't have macros. Most importantly, to another programmer, your if macro is indistinguishable from a built-in form in the language (in many Lisps, many special forms are in fact defined as macros based on a small number of lower level primitives or other forms). It is interesting to note that, in a language (such as Haskell) where side effects are restricted and non-termination is not a worry, your if function would likely be a perfectly fine replacement for the special form (though there are things you'd be able to do with Lisp macros that you wouldn't with Haskell functions). I'd recommend you to pick up a good Lisp book and try macros for yourself, since a well-written description followed by first-hand experience is likely to be a lot more effective than my lousy explanation at understanding what's special about macros (and Lisp in general). I believe "Why can't I have if as a function" is also one of the first exercises in SICP, which is a fantastic book for learning CS fundamentals if you are interested in becoming a better computer scientist.
Lady 3Jane.. · 2021-12-30 22:04
>>25595 Dead thread but get his most recent book: https://mitpress.mit.edu/books/software-design-flexibility absolutely insane information how to program a biological model
Arisu Mizuki · 2021-12-31 11:05
>>29252 I'm fudging a lot of the details here, but basically, Lisp macros 1. are inserted into your code at runtime rather than compile time, 2. are generated with ordinary Lisp code rather than a preprocessor like C, and 3. are a declared witin the body of your program. This allows you to do a lot of crazy soykaf. >Can you extend lisp from a language that uses ( brackets to one that uses [ via macros? or that has type checking etc? Yes and more. People have made macros that give lisp everything from infix operators to python-style indented blocks.
drop · 2021-12-31 11:23
>>29271 > I'm fudging a lot of the details here Yes, you fuarrrked up. > 1. are inserted into your code at runtime rather than compile time It's compile time. Though our first macro system was runtime, called MLAMBDA, which were first class. Introduced a &quote construct in formal parameter lists. newLisp (ironically) has them.
Gyula Kovacs · 2021-12-31 12:56
>>29252 >now this is what confuses me, now technically you could program any language into any language and extend C into Lisp etc, but this is obviously not what we are talking about. Lisp lets you define functions that run at compile time and don't evaluate their arguments: macros. Instead of evaluating the arguments, the macro gets the syntax tree of the argument. The macro returns a new syntax tree that replaces the preexisting one. Simple macro code resembles HTML template languages. (defmacro book-end (&body body) `(progn (print 'hello) ,@body (print 'bye))) The body variable is a list of all the arguments to the book-end macro. The back tick says "This is a literal piece of syntax, do not evaluate." Commas evaluate and interpolate values into the syntax. Example: ;; Original Code (book-end (print 'hi)) ;; After macro expansion (progn (print 'hello) (print 'hi) (print 'bye)) Now the code can run. Macros are basically hooks into the parser. You can think of them as compile time hooks. (Or, if you want to piss people off, you can think of macros as letting you compile at runtime. There's a whole can of worms when you consider the fact macros can use functions you define.) >Can you extend lisp from a language that uses ( brackets to one that uses [ via macros? or that has type checking etc? Yes, you can hook into the lexer with read macros. They work at level of individual characters. https://lisper.in/reader-macros
Zero Cool · 2022-01-22 06:26
Has anyone here been able to understand Sussman's Structure and Interpretation of Classical Mechanics? I've been trying to work through it but it seems like there's a level of knowledge I'm missing, in terms of physics, not algebra. Anyone have advice on how I can better wrap my head around what's discussed in the book?
Unknown · 2022-01-22 11:08
>>29588 It's the first time I hear this, would you recommend it to a physicist? If I understand correctly, it expresses physical equations and problems using scheme? Is it good?
DEBUG-CONTENTS · 2022-01-23 03:48
>>29591 It's specifically focused on classical mechanics (Langrian equations, rigid bodies, hamiltonian mechanics etc.), it assumes you know a lot of the prerequisite physics before diving in but presents a very nice system for operating on the equations involved (including simplification, latex display and substitution). I think if the physicist had some familiarity with scheme or programming it would likely be useful to them. Ultimately though because of my lack of knowledge I can't really say a huge amount more than that the programming side helped with symbolic manipulation well and that the physics was a little over my head.
Ivan Horvat · 2022-01-24 06:40
>>29588 >in terms of physics, not algebra Why do you mention algebra? Wouldn't it be more likely u would be missing some other math like calculus or something? >Anyone have advice on how I can better wrap my head around what's discussed in the book? I guess it depends on how what physics you know. What are some specific parts you don't understand?
Jet Black · 2022-02-06 20:17
>>25595 I'm a complete non-programmer who stupidly decided to read SICP as my introduction to CS. What the fuarrrk are Church numerals?
Mario Rossi · 2022-02-06 23:36
>>29993 They're a representation of numbers, most commonly done using only functions. Simply put, we name a state zero, and we have an increment function which just wraps around it. Here are the first three numbers with a nested representation: zero (zero) ((zero)) We can represent numbers like this, and define functions like addition using them, but it's clearly not practical. It's just a curiosity for the most part.
Hagbard Celine · 2022-02-09 01:58
>>29994 >It's just a curiosity for the most part. and tool of torturing brains of First Order Logic and Set Theory classes students at computer science faculties
Dujour · 2022-07-29 10:50
Any Schemer using scheme shell instead of more mainstream unix shells? https://scsh.net/