scheme to clojure translation
Hi All, I'm working through Brian Harvey's 61a 2008 SICP lecture (http:// www.youtube.com/watch?v=ljOrUCqsixs) and could really use a hand translating a couple of simple lines from scheme to clojure. Here is the code: 1: (define (compose f g) (lambda (x) (f (g x 2: (define (twice f) (compose f f)) 3: (define fourth (twice sq)) 4: (fourth 3) I've gotten this far and then I get stuck when I try to do line 2 from the above: 1: (defn compose [f g] #(f (g %))) Thanks in advance for any takers, Joshua -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en
Re: scheme to clojure translation
Hi Jim, Thanks so much! Using 'def' instead of 'defn' when defining a function composed of functions was what I was missing. I can't believe I spent an hour trying to figure this out -- it seems very obvious now. Doh! :) On Nov 8, 6:50 pm, Jim Crossley jcrossl...@gmail.com wrote: Hi Aquahappy joshua.ay...@gmail.com writes: [...] 1: (define (compose f g) (lambda (x) (f (g x 2: (define (twice f) (compose f f)) 3: (define fourth (twice sq)) 4: (fourth 3) I've gotten this far and then I get stuck when I try to do line 2 from the above: 1: (defn compose [f g] #(f (g %))) This is what I came up with. Note I also defined the sq function. user= (defn compose [f g] #(f (g %))) #'user/compose user= (defn twice [f] (compose f f)) #'user/twice user= (defn sq [x] (* x x)) #'user/sq user= (def fourth (twice sq)) #'user/fourth user= (fourth 3) 81 Jim -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en
Re: easy sum of squares refactor question
Thanks so much for your help!!! I'm so glad you had the time to respond to my newbie question. And as if you read my mind as I was going through the SICP lecture and referencing chapter two in Manning's Joy of Clojure book I was wondering how to turn this explicit recursive call taken from the scheme example into a Clojure non-tail recursion using 'recur'. I had been unsuccessful in my trials, so thank you times two!!! I'm off to try this out in my REPL! Many bows, Joshua On Oct 30, 11:29 pm, nchurch nchubr...@gmail.com wrote: Another solution, this time using Clojure's tail recursion: (defn sum2 [func incr a b] (loop [accum 0 x a] (if ( x b) accum (recur (+ (func x) accum) (incr x) This may be getting ahead of where you are now, so come back and look when you've covered map, reduce, and tail recursion! -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en
Re: Nested Syntax Quote, Quote and unquote example
Those are great links Linus, thank you. Also highly relevant and beginner friendly is the text adventure game chapter from the, Land of Lisp book, of which a free clojure translation is available online: http://www.lisperati.com/clojure-spels/casting.html Cheers, Joshua On Oct 31, 6:01 pm, Linus Ericsson oscarlinuserics...@gmail.com wrote: No one can be told what the matrix is. Well, the best way to understand the macros is to try it out yourself. John Lawrence Aspden published some really helpful tutorials om macros, and a kata - try it! Links in the end. There are some other code examples around, also look in the source-code of clojure core and various clojure contrib libraries for a lot of examples, some of which you have to contemplate and come back to later. They have some macros in the code. Also beware that you most often don't want to use macros in the first place, since you can solve most problems with the incredible powerful functions as well. Last but not least I would recommend getting some good books on Clojure, I started with Programming Clojure of Stuart Halloway and it was really helpful for me. Most problems can be expressed as a sequences of key-value-maps to which you apply map and reduce (or nowadays protocols for more speed). Be prepared to spend some time getting your head around the transition to functional, lazy, immutable code with macro-programming, it will be worth it. You will still have good use of you java-knowledge, but will probably structure your programs differently. I also had great help of the beginning of Paul Grahams Practical Common Lisp avail free at:http://pragprog.com/book/shcloj/programming-clojure Macro kata:http://www.learningclojure.com/2010/11/syntax-quote-kata-for-confused... Macro tutorial:http://www.learningclojure.com/2010/09/clojure-macro-tutorial-part-i-... Clojure source code repositories:https://github.com/clojure/ Programming Clojure:http://pragprog.com/book/shcloj/programming-clojure /Linus 2011/10/31 vikbehal vikbe...@gmail.com Can anyone give me some basic example which contained nested quotes and unquotes? with explanation at each step! -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en
easy sum of squares refactor question
Hi All, I'm watching Brian Harvey's SICP lecture #3 from Berkeley 61A/Spring 2011 and had a question about how I could refactor the following function so that the (+a 1) can be abstracted to be a function and passed in. Here is the original: (defn square [x] (* x x)) (defn sum [fn a b] (if ( a b) 0 (+ (fn a) (sum fn (+ a 1) b = (sum square 2 4) 29 That works. So then I want to replace (+ a 1) with a function like: (defn addone [a] (inc a)) but I'm not sure how this would be structured. I'm new to clojure and realize this is a stepping stone to learning about higher order functions so apologies in advance for any code ugliness due to my newness to clojure. Thanks for your help!!! -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en