sum(range(1,11))
I think this more explanatory than the Lisp. Perhaps that's just me. sum(range(1,11))
I think this more explanatory than the Lisp. Perhaps that's just me.Aren't the integers the domain over which we are operating?
How is the comma obvious from the problem statement?
If I can assume a person knows a particular language, the J version is perfectly clear.
+/ 1+ i.10But yes, I acknowledge that every programming language requires a certain amount of knowledge about generalities, Python being no exception.
And I really should sit down at some point and learn an APL variant.
[+] 1 ... 10If we refuse to normalize the languages by defining `sum`, then it’s an API battle... And Clojure/Clojurescript has easy access to Java and Javascript. (Plus if we count Clojure on .NET...)
from https://www.python.org/dev/peps/pep-0020/ :
> There should be one-- and preferably only one --obvious way to do it.
I don't know if common lisp is an "easier" lisp, but I sure would like to have a watered down version of lisp so I can learn its paradigms. I don't even know if that's what haskell is.
Python is designed to be human readable.
Here's an old blog post by me, which may be hard to jump into because its part of a longer conversation on proggit or something, but anyway: http://williamedwardscoder.tumblr.com/post/18319031919/progr...
Python has more syntax to remember than in Lisp, but ends up being a lot more readable (eg lists, dicts etc)
Clojure does them right, imho.
Otherwise, I worked through SICP a couple years back, and I found it invaluable to getting familiar with Lisps and functional programming in general.
I remember sitting down one day (before working through SICP) and saying, "I'm great at PHP, I'll probably just be able to write a Clojure project off the top of my head!" Cue clown music as I beat my head against a language much more expressive than anything I'd encountered to date.
It was a few solid weeks of effort before I could get anywhere, but I remember remarking at the time, "if I can learn this well enough, I'll be a wizard!"
I tried reading lisp courses or tutorials, it really did not help. It's either too steep, or it just talks about simple things like adding numbers like (+ 5 9) I'm more into what makes me understand lisp as a better language.
I started with Land of Lisp, but it felt very arcane to me at the time. I would copy in code from the book, and then look back at just jibberish. I gave up on that a few chapters in. Then I made that site in Clojure, one error at a time, pounding like a monkey until it worked. I then read Clojure Programming twice, followed by working through Let Over Lambda. About halfway through Let Over Lambda, I think it "clicked". I finished with an amazing online course in Programming Languages through Brown University. I finished with SICP chapters 1-3.
Nothing worthwhile is ever easy. If you need any help please reach out, you can't waste my time.
1) Install Racket (http://racket-lang.org/) 2) Read Simply Scheme (https://www.cs.berkeley.edu/~bh/ss-toc2.html) by Brian Harvey and Matthew Wright. It's free. Harvey taught the Scheme classes at UC Berkeley for many years, and is an outstanding (and award-winning) teacher (he also has a series of videos that might be helpful).
One of the stated goals of Harvey and Wright's approach is to get the student ready for SICP. It's definitely way above the "add two numbers" level, but is not quite the instant warp factor 10 that you get when you dive into SICP.
If you do this, you should also use the Racket Library (https://www.hashcollision.org/simply-scheme/) that adds all the extra stuff used in the Harvey and Wright text.
Good luck!
The pattern of adding numbers is just an instance of a closed set recursion.
(F e0 ... en) -> (reduce F (e0 ... en) zero-element) -> (F(...(F(F zero-element e0) e1)...) en)
Not just about simple addition, it works with function relating two elements of the same kind to another element of that kind. From 2 arguments to 1, so you can pick another element and relate it to the previous result, so on and so forth until you reduced a list of elements to just one.
Of course it's not an application like crafting a website, or rendering GUI, or manipulating sound samples, but these principles are everywhere.
Fully working lisp in 10 (easy to not so easy) steps.
First, you should pick a Lisp-1 because otherwise you'll be hit with "weird" behaviour earlier than necessary.
Next, learn the syntax of your Lisp. Pick one that doesn't have many extensions, as they may become a distraction. Ignore any macros your Lisp supports, leave quasiquote and unquote for later (you can use `quote`, but as a shorthand for the `list` function at first). Learning the syntax won't take long, as it becomes pretty minimal if you omit those things.
Make sure you use an editor that understands your Lisp and has advanced syntax highlighting and auto indent functionality. The difference between a well formatted and highlighted code and a blob of flat text is huge in Lisps, compare this:
(define (plot-lines data)
(parameterize ([plot-x-tick-label-anchor 'top-right]
[plot-x-tick-label-angle 35])
(let ([out-path (build-path (current-directory) "lines.png")]
[data (list (tick-grid)
(discrete-histogram data #:y-min 0 #:y-max 100))])
(plot data
#:y-label "sloc"
#:x-label #f
#:out-file out-path))))
with the same code properly highlighted: https://klibert.pl/statics/images/rkt-highlighting.pngSimilarly, don't ever attempt to balance parens manually, your editor should handle this, along with commands for expanding and shrinking particular expresions (for example, change "(message "%s") something" into "(message "%s" something)" with a single keypress) and other goodies.
After the groundwork is done, start with control-flow constructs (forms in Lisp parlance): constructs for assignement (define/defvar/setq (but not! setf)/etc.), for branching (if/cond/etc.), for grouping (begin/progn/etc.), for looping/iteration (loop/for/dotimes/etc.) and error handling (with-handlers/condition-case/etc.). Learn basic data constructors (list/vector/hashmap/etc.). Experiment with these in the REPL or write some simple scripts using them. Learn how to define functions/procedures and refactor your scripts to use them.
You're basically done at this point. Not in the sense that you know Lisp, but you're ready to learn all there is to Lisp without much hassle. The last obstacle is learning about defmacro and quasiquote/unquote, but that's easy once you're comfortable with lists and iteration. Everything else in most Lisps is built with macros, and at that point you have all the tools needed to write Lisp code, to investigate your Lisp implementation and finally to build your own extensions into the language.
I think that most of the difficulty of learning Lisp stems from learning things in wrong order. Lispers naturally want to show you the most powerful language features to distinguish Lisps from other languages, but they already forgot that these features are built on much simpler things. Or, on the other hand, they drift into lambda calculus and Peano numbers, which smells like a Turing tarpit, where nothing of interest is easy to do.
Anyway, try learning things in that order, maybe it'll help. It worked for me, after years of approaching Lisp and quickly retreating I was finally able to grok it thanks to this approach. YMMV of course.
[EDIT: formatting]
Close match to the Python above:
(apply + (range 1 11))
Beginners probably are better of using scheme or racket.
Common Lisp does not force you to use FORMAT. It has a bunch of simpler IO functions, too.
times(range(1,11)) ?
--- (apply #'* (-range 1 11))
python is weirdly halfway on 'math oriented' abstractions. Python reached version 1.0 in January 1994. The major new features
included in this release were the functional programming tools
lambda, map, filter and reduce. Van Rossum stated that "Python
acquired lambda, reduce(), filter() and map(), courtesy of a Lisp
hacker who missed them and submitted working patches".[9]
https://en.wikipedia.org/wiki/History_of_Python#Version_1.0 from functools import reduce
from operators import mul
def times(data):
return reduce(mul, data) >>> times([])
TypeError: reduce() of empty sequence with no initial value
from functools import reduce
from operators import mul
def times(data):
return reduce(mul, data)
Proper neutral element: >>> times = lambda l: reduce(mul, l,1)
>>> times([])
1 >>> def times(input):
... return reduce(lambda x, y: x*y, input, 1)
...
>>> times(range(1,11))
3628800 >>> Prod = lambda xs: fp.reduce(op.mul, xs, 1)
>>> Prod(range(1,11))
362880