Re: Correct way to define the else clause of a cond form?
On 7 July 2011 09:10, Ken Wesson wrote: > On Thu, Jul 7, 2011 at 2:47 AM, Tassilo Horn wrote: >> Ken Wesson writes: >> >> Hi! >> Remove the question mark... or whatever this is...  and you'll be fine. >>> >>> Whatever WHAT is? There's nothing in your post there but three blank >>> lines. >> >> Sure there is! ;-) >> >> , >> | character:  (65532, #o14, #xfffc) >> | preferred charset: unicode (Unicode (ISO10646)) >> | code point: 0xFFFC >> | syntax: w which means: word >> | category: .:Base >> | buffer code: #xEF #xBF #xBC >> | file code: #xEF #xBF #xBC (encoded by coding system utf-8-emacs) >> | display: by this font (glyph code) >> | xft:-unknown-GFS Bodoni >> Rg-normal-normal-normal-*-13-*-*-*-*-0-iso10646-1 (#x226) >> | >> | Character code properties: customize what to show >> | name: OBJECT REPLACEMENT CHARACTER >> | general-category: So (Symbol, Other) >> ` > > Nothing like that is there. Just this: > > > and the only reason anything at all is visible is because it's gotten > quoted three levels deep. No, there's definitely a character there. For some reason your mail reader/browser is not displaying it. In your quote above, though, there is indeed nothing. -- Michael Wood -- 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: Correct way to define the else clause of a cond form?
On Thu, Jul 7, 2011 at 2:47 AM, Tassilo Horn wrote: > Ken Wesson writes: > > Hi! > >>> Remove the question mark... or whatever this is... >>> >>>  >>> >>> and you'll be fine. >> >> Whatever WHAT is? There's nothing in your post there but three blank >> lines. > > Sure there is! ;-) > > , > | character:  (65532, #o14, #xfffc) > | preferred charset: unicode (Unicode (ISO10646)) > | code point: 0xFFFC > | syntax: w which means: word > | category: .:Base > | buffer code: #xEF #xBF #xBC > | file code: #xEF #xBF #xBC (encoded by coding system utf-8-emacs) > | display: by this font (glyph code) > | xft:-unknown-GFS Bodoni > Rg-normal-normal-normal-*-13-*-*-*-*-0-iso10646-1 (#x226) > | > | Character code properties: customize what to show > | name: OBJECT REPLACEMENT CHARACTER > | general-category: So (Symbol, Other) > ` Nothing like that is there. Just this: >>> >>> >>> and the only reason anything at all is visible is because it's gotten quoted three levels deep. -- Protege: What is this seething mass of parentheses?! Master: Your father's Lisp REPL. This is the language of a true hacker. Not as clumsy or random as C++; a language for a more civilized age. -- 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: Correct way to define the else clause of a cond form?
Ken Wesson writes: Hi! >> Remove the question mark... or whatever this is... >> >>  >> >> and you'll be fine. > > Whatever WHAT is? There's nothing in your post there but three blank > lines. Sure there is! ;-) , | character:  (65532, #o14, #xfffc) | preferred charset: unicode (Unicode (ISO10646)) |code point: 0xFFFC |syntax: w which means: word | category: .:Base | buffer code: #xEF #xBF #xBC | file code: #xEF #xBF #xBC (encoded by coding system utf-8-emacs) | display: by this font (glyph code) | xft:-unknown-GFS Bodoni Rg-normal-normal-normal-*-13-*-*-*-*-0-iso10646-1 (#x226) | | Character code properties: customize what to show | name: OBJECT REPLACEMENT CHARACTER | general-category: So (Symbol, Other) ` Bye, Tassilo -- 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: Correct way to define the else clause of a cond form?
On Jul 6, 8:08 pm, David Sletten wrote: > On Jul 6, 2011, at 10:58 PM, Conrad Taylor wrote: > > > > > > > > > > > On Jul 6, 7:33 pm, Benny Tsai wrote: > >> Could you please post the entire form, including the code surrounding the > >> cond form (since total, amount, and country need to be defined somewhere)? > > > Benny, that was just sample code to zero in on the initial issue. I'm > > working > > through the SICP with a lot of pain but here's what I have so far: > > > (def us-coins (list 50 25 10 5 1)) > > (def uk-coins (list 100 50 20 10 5 2 1 0.5)) > > > (defn first-denomination [ coin-values ] (first coin-values)) > > > (defn except-first-denomination [ coin-values ] (rest coin-values)) > > > (defn no-more? [coin-values] (nil? coin-values)) > > > (defn cc [amount coin-values] > > (cond > > (= amount 0) 1 > > (or (< amount 0) (no-more? coin-values)) 0 > >  :else (+ (cc amount (except-first-denomination > > coin-values)) > > (cc (- amount (first-denomination coin- > > values)) coin-values > > Part of the problem is that Clojure uses a slightly different syntax for > 'cond' than Scheme (and Common Lisp) do. In particular, Common Lisp more > frequently allows for side effects, so rather than a single consequent value > Common Lisp's COND encloses its consequent expressions in a an additional > layer of parentheses. Clojure discourages side effects, so it's reasonable to > think in terms of a single expression as a consequent. The need for enclosing > parentheses disappears. > > Beware that the predicate 'nil?' tests whether an object is 'nil'. You > probably want to use the predicate 'empty?' to test whether your coin list is > empty. > > Have all good days, > David Sletten David, thanks for the advice here and things appear to be working now. It's not optimized but it's functional. Anyway, I don't if the tabs caused issues for Clojure but I know I had issue with other languages. Thanks all, -Conrad -- 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: Correct way to define the else clause of a cond form?
On Wednesday, July 6, 2011 9:06:30 PM UTC-6, Tim Robinson wrote: > > You have some rogue text cluttering your cond statement. > Remove the question mark... or whatever this is... > >  > > and you'll be fine. > That's what I encountered too. Conrad, when I pasted your code into emacs, there was a mystery character between the 0 at the end of the second condition and the :else at the start of the default condition. Once I removed that character (and fixed a spurious newline in the last line), the code evaluated with no problems. -- 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: Correct way to define the else clause of a cond form?
Lol. not sure what to tell you... on Mac OSX Firefox I see what looks like this -- - OBJ - -- in the middle line, but really really really small. and when I copy his text and paste at the repl, I get his same error. When I remove it its not a problem. On Jul 6, 9:07 pm, Ken Wesson wrote: > On Wed, Jul 6, 2011 at 11:06 PM, Tim Robinson wrote: > > You have some rogue text cluttering your cond statement. > > Remove the question mark... or whatever this is... > > >  > > > and you'll be fine. > > Whatever WHAT is? There's nothing in your post there but three blank lines. > > -- > Protege: What is this seething mass of parentheses?! > Master: Your father's Lisp REPL. This is the language of a true > hacker. Not as clumsy or random as C++; a language for a more > civilized age. -- 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: Correct way to define the else clause of a cond form?
On Jul 6, 2011, at 10:58 PM, Conrad Taylor wrote: > On Jul 6, 7:33 pm, Benny Tsai wrote: >> Could you please post the entire form, including the code surrounding the >> cond form (since total, amount, and country need to be defined somewhere)? > > Benny, that was just sample code to zero in on the initial issue. I'm > working > through the SICP with a lot of pain but here's what I have so far: > > (def us-coins (list 50 25 10 5 1)) > (def uk-coins (list 100 50 20 10 5 2 1 0.5)) > > (defn first-denomination [ coin-values ] (first coin-values)) > > (defn except-first-denomination [ coin-values ] (rest coin-values)) > > (defn no-more? [coin-values] (nil? coin-values)) > > (defn cc [amount coin-values] > (cond > (= amount 0) 1 >(or (< amount 0) (no-more? coin-values)) 0 >  :else (+ (cc amount (except-first-denomination coin-values)) > (cc (- amount (first-denomination coin- > values)) coin-values > Part of the problem is that Clojure uses a slightly different syntax for 'cond' than Scheme (and Common Lisp) do. In particular, Common Lisp more frequently allows for side effects, so rather than a single consequent value Common Lisp's COND encloses its consequent expressions in a an additional layer of parentheses. Clojure discourages side effects, so it's reasonable to think in terms of a single expression as a consequent. The need for enclosing parentheses disappears. Beware that the predicate 'nil?' tests whether an object is 'nil'. You probably want to use the predicate 'empty?' to test whether your coin list is empty. Have all good days, David Sletten -- 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: Correct way to define the else clause of a cond form?
On Wed, Jul 6, 2011 at 11:06 PM, Tim Robinson wrote: > You have some rogue text cluttering your cond statement. > Remove the question mark... or whatever this is... > >  > > and you'll be fine. Whatever WHAT is? There's nothing in your post there but three blank lines. -- Protege: What is this seething mass of parentheses?! Master: Your father's Lisp REPL. This is the language of a true hacker. Not as clumsy or random as C++; a language for a more civilized age. -- 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: Correct way to define the else clause of a cond form?
You have some rogue text cluttering your cond statement. Remove the question mark... or whatever this is...  and you'll be fine. On Jul 6, 8:58 pm, Conrad Taylor wrote: > On Jul 6, 7:33 pm, Benny Tsai wrote: > > > Could you please post the entire form, including the code surrounding the > > cond form (since total, amount, and country need to be defined somewhere)? > > Benny, that was just sample code to zero in on the initial issue. I'm > working > through the SICP with a lot of pain but here's what I have so far: > > (def us-coins (list 50 25 10 5 1)) > (def uk-coins (list 100 50 20 10 5 2 1 0.5)) > > (defn first-denomination [ coin-values ] (first coin-values)) > > (defn except-first-denomination [ coin-values ] (rest coin-values)) > > (defn no-more? [coin-values] (nil? coin-values)) > > (defn cc [amount coin-values] > (cond > (= amount 0) 1 > (or (< amount 0) (no-more? coin-values)) 0 >  :else (+ (cc amount (except-first-denomination coin-values)) > (cc (- amount (first-denomination coin- > values)) coin-values -- 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: Correct way to define the else clause of a cond form?
On Jul 6, 7:33 pm, Benny Tsai wrote: > Could you please post the entire form, including the code surrounding the > cond form (since total, amount, and country need to be defined somewhere)? Benny, that was just sample code to zero in on the initial issue. I'm working through the SICP with a lot of pain but here's what I have so far: (def us-coins (list 50 25 10 5 1)) (def uk-coins (list 100 50 20 10 5 2 1 0.5)) (defn first-denomination [ coin-values ] (first coin-values)) (defn except-first-denomination [ coin-values ] (rest coin-values)) (defn no-more? [coin-values] (nil? coin-values)) (defn cc [amount coin-values] (cond (= amount 0) 1 (or (< amount 0) (no-more? coin-values)) 0  :else (+ (cc amount (except-first-denomination coin-values)) (cc (- amount (first-denomination coin- values)) coin-values -- 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: Correct way to define the else clause of a cond form?
On Jul 6, 7:16 pm, David Sletten wrote: > Conrad, > > The syntax of 'cond' is actually pretty straightforward. Following the symbol > 'cond' you have pairs of predicate forms and consequent expressions. The > 'cond' form evaluates each predicate in turn until one evaluates to true and > then returns the value of the corresponding consequent form. Many 'cond' > forms have a default value that is returned when none of the predicates > succeed. Remember that in Clojure only the value 'false' and the value 'nil' > are considered false. Everything else is true. By convention we use the > keyword ':else' as a 'predicate' for the default case. Since ':else' is > neither 'false' nor 'nil', it is considered to be true, and if the 'cond' > form reaches the ':else' clause, then the default value will be returned. > > Given these restrictions, only a) and c) are syntactically correct. And even > they don't do what you want. > > > a) > > > (cond > > (= total 20) 8.75 > > (or (amount > 20) (= country "US") 9.75) > > (else 10.0)) > > Here we have: > predicate | consequent > (= total 20) | 8.75 > (or (amount > 20) (= country "US") 9.75) | (else 10.0) > > Unfortunately, (else 10.0) winds up as the consequent of the 2nd predicate. > Furthermore, 'else' is not a Clojure operator. Unless you've defined a > function or macro named 'else' you will get an error. > > > b) > > > (cond > > (= total 20) 8.75 > > (or (amount > 20) (= country "US") 9.75) > > :default 10.0) > > predicate | consequent > (= total 20) | 8.75 > (or (amount > 20) (= country "US") 9.75) | :default > 10.0 | ??? > > These are not paired up properly. > > > c) > > > (cond > > (= total 20) 8.75 > > (or (amount > 20) (= country "US") 9.75) > > 10.0 ) > > predicate | consequent > (= total 20) | 8.75 > (or (amount > 20) (= country "US") 9.75) | 10.0 > > Not what you expected... > > > > > d) > > > (cond > > (= total 20) 8.75 > > (or (amount > 20) (= country "US") 9.75) > > :else 10.0 ) > > predicate | consequent > (= total 20) | 8.75 > (or (amount > 20) (= country "US") 9.75) | :else > 10.0 | ??? > > Not syntactically correct. > > Here's what you want to use: > (cond > (== total 20) 8.75 > (or (> amount 20) (= country "US")) 9.75 > :else 10.0) > > (Note that '==' is the proper predicate for numerical equality.) > Is this true for comparing integer values? I ask this question because I read the following in the documentation: http://clojuredocs.org/clojure_core/clojure.core/= -Conrad > Have all good days, > David Sletten -- 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: Correct way to define the else clause of a cond form?
Could you please post the entire form, including the code surrounding the cond form (since total, amount, and country need to be defined somewhere)? -- 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: Correct way to define the else clause of a cond form?
On Jul 6, 5:34 pm, Conrad Taylor wrote: > Hi, what's the correct way to define an else clause of a cond form? > For example, > > a) > > (cond > (= total 20) 8.75 > (or (amount > 20) (= country "US") 9.75) > (else 10.0)) > > b) > > (cond > (= total 20) 8.75 > (or (amount > 20) (= country "US") 9.75) > :default 10.0) > > c) > > (cond > (= total 20) 8.75 > (or (amount > 20) (= country "US") 9.75) > 10.0 ) > > d) > > (cond > (= total 20) 8.75 > (or (amount > 20) (= country "US") 9.75) > :else 10.0 ) I'm getting the error message at the line where cond is being defined: Unable to resolve symbol: ? in this context At line 14, I have the following: (cond This is kind of cryptic as it can get being that this is defined by the core language unless I need to import a library/module/form. >From the documentation, I have everything correct but I'm seeing two different errors for 1.2.0 and 1.3.0. -Conrad -- 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: Correct way to define the else clause of a cond form?
Conrad, The syntax of 'cond' is actually pretty straightforward. Following the symbol 'cond' you have pairs of predicate forms and consequent expressions. The 'cond' form evaluates each predicate in turn until one evaluates to true and then returns the value of the corresponding consequent form. Many 'cond' forms have a default value that is returned when none of the predicates succeed. Remember that in Clojure only the value 'false' and the value 'nil' are considered false. Everything else is true. By convention we use the keyword ':else' as a 'predicate' for the default case. Since ':else' is neither 'false' nor 'nil', it is considered to be true, and if the 'cond' form reaches the ':else' clause, then the default value will be returned. Given these restrictions, only a) and c) are syntactically correct. And even they don't do what you want. > a) > > (cond > (= total 20) 8.75 > (or (amount > 20) (= country "US") 9.75) > (else 10.0)) > Here we have: predicate | consequent (= total 20) | 8.75 (or (amount > 20) (= country "US") 9.75) | (else 10.0) Unfortunately, (else 10.0) winds up as the consequent of the 2nd predicate. Furthermore, 'else' is not a Clojure operator. Unless you've defined a function or macro named 'else' you will get an error. > b) > > (cond > (= total 20) 8.75 > (or (amount > 20) (= country "US") 9.75) > :default 10.0) > predicate | consequent (= total 20) | 8.75 (or (amount > 20) (= country "US") 9.75) | :default 10.0 | ??? These are not paired up properly. > c) > > (cond > (= total 20) 8.75 > (or (amount > 20) (= country "US") 9.75) > 10.0 ) predicate | consequent (= total 20) | 8.75 (or (amount > 20) (= country "US") 9.75) | 10.0 Not what you expected... > > d) > > (cond > (= total 20) 8.75 > (or (amount > 20) (= country "US") 9.75) > :else 10.0 ) predicate | consequent (= total 20) | 8.75 (or (amount > 20) (= country "US") 9.75) | :else 10.0 | ??? Not syntactically correct. Here's what you want to use: (cond (== total 20) 8.75 (or (> amount 20) (= country "US")) 9.75 :else 10.0) (Note that '==' is the proper predicate for numerical equality.) Have all good days, David Sletten -- 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: Correct way to define the else clause of a cond form?
On Jul 6, 6:07 pm, Benny Tsai wrote: > I believe (d) is considered the idiomatic way*. Btw, I think the second > case may not be written correctly; if the intended logic is that 9.75 should > be returned when either amount > 20 or country = "US", the code should look > something like this: > > (cond (= total 20) 8.75 > (or (> amount 20) (= country "US")) 9.75 > :else 10.0) > > *See:http://dev.clojure.org/display/design/Library+Coding+Standards Benny, thanks for the reference. -Conrad -- 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: Correct way to define the else clause of a cond form?
In addition to Benny's suggestion - I will suggest, for future reference, that the ClojureDocs website does an brilliant job in showing some examples. It really is a valuable resource that I've come to rely on. http://clojuredocs.org/clojure_core/clojure.core/cond And what's interesting to note is the comment made near the bottom: "We should add a comment in the docstring for the final usage of :else." Isn't that a coincidence :) On Jul 6, 6:34 pm, Conrad Taylor wrote: > Hi, what's the correct way to define an else clause of a cond form? > For example, > > a) > > (cond > (= total 20) 8.75 > (or (amount > 20) (= country "US") 9.75) > (else 10.0)) > > b) > > (cond > (= total 20) 8.75 > (or (amount > 20) (= country "US") 9.75) > :default 10.0) > > c) > > (cond > (= total 20) 8.75 > (or (amount > 20) (= country "US") 9.75) > 10.0 ) > > d) > > (cond > (= total 20) 8.75 > (or (amount > 20) (= country "US") 9.75) > :else 10.0 ) -- 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: Correct way to define the else clause of a cond form?
I believe (d) is considered the idiomatic way*. Btw, I think the second case may not be written correctly; if the intended logic is that 9.75 should be returned when either amount > 20 or country = "US", the code should look something like this: (cond (= total 20) 8.75 (or (> amount 20) (= country "US")) 9.75 :else 10.0) *See: http://dev.clojure.org/display/design/Library+Coding+Standards -- 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
Correct way to define the else clause of a cond form?
Hi, what's the correct way to define an else clause of a cond form? For example, a) (cond (= total 20) 8.75 (or (amount > 20) (= country "US") 9.75) (else 10.0)) b) (cond (= total 20) 8.75 (or (amount > 20) (= country "US") 9.75) :default 10.0) c) (cond (= total 20) 8.75 (or (amount > 20) (= country "US") 9.75) 10.0 ) d) (cond (= total 20) 8.75 (or (amount > 20) (= country "US") 9.75) :else 10.0 ) -- 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