Fork me on GitHub
#beginners
<
2015-11-22
>
roelof11:11:26

what is wrong here : (= "longest" (reduce (fn [a b] (if (< a b) b a)) ["which" "word" "is" "longest"]))

ian11:11:39

@roelof: if you try to run that in a repl it should give you a reasonably helpful error message.

ian11:11:59

@roelof: it seems, from some of the issues you have, that you're not using Light Table's major selling point, which is the instarepl - it shows you values and errors inline, which should help with some of the problems you're facing.

roelof13:11:45

oke, I see on instarepl that only the first two words are used

roelof13:11:00

so it seems a parentheses problem somehow

chris16:11:01

@roelof: idk if you've solved it yet, but your fn parens are wrong. You need another close paren after the if

roelof16:11:04

I did not solved it yet

roberto16:11:48

you can’t compare strings like that

roberto16:11:57

that should be a clue

roberto16:11:14

(< “a” “b”) => ClassCastException java.lang.String cannot be cast to java.lang.Number http://clojure.lang.Numbers.lt (Numbers.java:221)

roelof18:11:21

I did solve it . I needed to use the length of a and b and compare those

roelof18:11:38

what is the meaning of this exercise :

roelof18:11:06

"Multimethods allow more complex dispatching" (= "Bambi eats veggies." (diet {:species "deer" :name "Bambi" :age 1 :eater :herbivore}))

swizzard19:11:27

so multimethods are defined with a dispatch-fn

swizzard19:11:01

and then different function bodies are invoked depending on the return value of the dispatch-fn

roelof19:11:26

I found out that I need to add something here

roelof19:11:27

(defmulti diet (fn [x] (:eater x))) (defmethod diet :herbivore [a] "veggies") (defmethod diet :carnivore [a] "animals") (defmethod diet :default [a] "I don't know what a eats")

roelof19:11:46

but this one is not good

swizzard19:11:49

do you know that keywords are functions?

roelof19:11:16

nope, I just begin learning clojure with the clojure koans

swizzard19:11:33

so (fn [x] (:eater x))

swizzard19:11:48

can just be rewritten as :eater

roelof19:11:25

yes, and later on eater is given by a name like bambi which is a herbivore

swizzard19:11:41

well that’s a map, right?

swizzard19:11:22

so you have your map, m

swizzard19:11:01

if you call (:eater m), it’ll return :herbivore

swizzard19:11:40

when you call a multimethod, it’ll call the dispatch-fn, and then call the method that matches the result

swizzard19:11:28

so it’ll call (:eater m), which’ll return :herbivore, and will then call the version of diet that corresponds to :herbivore

roelof19:11:28

and the return value needs to be veggies according to this : Bambi eats veggies."

swizzard19:11:09

or does it need to be “Bambi eats veggies”?

roelof19:11:41

you are right

roelof19:11:01

where bambi is the value of the :name part of the map

roelof19:11:13

oke, so I can do this str ( (:name x) "eats veggies"))

swizzard19:11:49

well, provided you move the parens around

roelof19:11:32

oke, there is one ) to much

cjmurphy19:11:01

str is a function and there is only one way to invoke a function

swizzard19:11:41

you just had the str outside the parens

roelof19:11:28

bummer, still not good

roelof19:11:56

still a false

roelof19:11:22

with this code :

roelof19:11:23

(defmulti diet (fn [x] (:eater x))) (defmethod diet :herbivore [a] (str ( (:name x) " " "eats veggies.")))

swizzard19:11:28

do you have access to a repl?

roelof19:11:27

yep, I use Light table and use now a instarepl

roelof19:11:55

I have changed x in a but then I see this message : java.lang.ClassCastException: java.lang.String cannot be cast to clojure.lang.IFn

roelof19:11:33

so I have to figure out what a has as value

swizzard19:11:51

look at your code

swizzard19:11:01

think about what (:name x) resolves to

roelof19:11:33

is schould be the name of the animal

swizzard19:11:52

so when you have ((:name x)…

swizzard19:11:52

it’s gonna turn into (“bambi”…), right?

roelof19:11:31

right , except x is unknown in that functioon

roelof19:11:44

I think it suppose to be a

swizzard19:11:01

but think about the error that was thrown

swizzard19:11:17

java.lang.String cannot be cast to clojure.lang.IFn

roelof19:11:41

yes, clojure think that the string is a function

roelof19:11:24

but how to solve that

swizzard19:11:49

why would clojure try to use a string as a function?

roelof19:11:30

aha, it's in the (:name x) part : name is not a function

swizzard19:11:58

it’s technically not a function

swizzard19:11:05

but it’s a keyword, which can work as a function

roelof19:11:25

clojure is sometimes very confusing

ian19:11:39

A keyword is a function in the sense that it implements IFn.

swizzard19:11:41

i will say clojure’s error messages are not my favorite part of the language

swizzard19:11:13

so that error message

swizzard19:11:31

java.lang.String doesn’t implement IFn

roelof19:11:08

oke, the str part str ( (:name part) ....

roelof19:11:01

maybe I use join to join the :name part and the rest of the requested output ?

swizzard19:11:02

so try something like (“foo” 1 2 3) in your repl

roelof19:11:37

I see the same error message

swizzard19:11:05

which suggests that the same kind of thing is happening in your code

roelof19:11:34

so it do not use str here

swizzard19:11:00

it’s not the call to str

swizzard19:11:30

((str +) 1 2 3)

swizzard19:11:01

vs, e..g, (str (+ 1 2 3))

roelof19:11:59

the last one gives a answer the last one gives the same error message

roelof19:11:14

sorry but I still no see the answer

swizzard19:11:04

so your code is still (defmethod diet :herbivore [a] (str ( (:name x) " " "eats veggies."))), right?

roelof19:11:43

this is working : (defmethod diet :herbivore [a] (str (:name a) " " "eats veggies"))

swizzard19:11:04

still not sure what all those “s are doing in there

swizzard19:11:10

but if it works, it works simple_smile

roelof19:11:45

I have to put some spaces in it. Otherwise I see bambieats instead of bambi eats

swizzard19:11:06

but why not “ eats veggies.”

swizzard19:11:51

as opposed to ” “ “eats veggies.”

roelof19:11:09

also a possibility

roelof19:11:24

I now see what I did wrong , too much parentenses

swizzard19:11:04

easy mistake to make simple_smile