Fork me on GitHub
#beginners
<
2015-11-24
>
roelof07:11:11

good morning all

roelof07:11:47

anyone who can tell me what is wrong with this code

roelof07:11:48

(= 20 (do (dosync (alter the-world ( fn[f] (f 20) )))))

roelof07:11:48

I see this error message : java.lang.ClassCastException: java.lang.Long cannot be cast to clojure.lang.IFn

andrut07:11:07

what is the-world?

roelof07:11:51

this is the world : (def the-world (ref "hello"))

roelof07:11:10

but on a earlier exercise the value is set to 0 (zero)

roelof07:11:29

@andrut : is this what you meant

andrut07:11:38

yes, it's what I meant simple_smile

andrut07:11:13

I think the argument that you're passing to the anonymous function ( [f]) is not a function but it's a number

roelof07:11:56

yes, it is . I tried to "overwrite" the zero with 20 like I did on earlier koans

andrut07:11:22

so instead of invoking f, you could rather use another function (operator function), to make this koan work

roelof07:11:41

yes, I think I can add 20

andrut07:11:47

assuming that the value is 0, it should be straightforward simple_smile

roelof07:11:28

(fn [f) ( + 20 ?? )

roelof07:11:44

I have to think what is in the ???

agile_geek07:11:49

@roelof: think about what value ‘f’ will have

roelof08:11:00

the orginal value does not have a name

agile_geek08:11:06

look up what alter takes as an argument

agile_geek08:11:52

@roelof: you have given it a name locally in your fn

roelof08:11:20

then it's f

roelof08:11:30

so (+ 20 f)

agile_geek08:11:03

Remember the REPL is your friend.

roelof08:11:40

I did and the koan is successfull

roelof08:11:42

I did understand a earlier one not right. I see that it have the same layout ( function arguments)

roelof08:11:54

thanks all again

agile_geek08:11:11

@roelof: not sure if you are doing this but I found it useful to have a repl open alongside Koans to allow me to experiment.

roelof08:11:56

I also. I use Lighttable so I can evulate things quick. But sometimes I loose track and then the repl cannot help me

roelof08:11:21

and I want to understand why things are working. Not just find it by luck

roelof14:11:28

Anyone who can help me with this one : https://www.refheap.com/112021

swizzard14:11:39

the error message looks pretty helpful on that one

roelof14:11:37

good afternoon

roelof14:11:59

yes, the number of arguments does not fit

roelof14:11:19

but still I have no clue where I made a mistake

swizzard14:11:44

check your parens?

roelof14:11:35

oke, the same as I made a lot before it , Stupid parentheses

swizzard14:11:46

yeah it’s irritating sometimes

swizzard14:11:49

but they’re worth it!

roberto14:11:55

now that you are starting to feel the pain of parens, try learning paredit

swizzard14:11:56

i like to break stuff like this into as many lines as i can get away with

roelof14:11:29

Does paredit exists for Lightable and is it somehow else then Rainbow parenthenses

roberto14:11:27

I don’t know. I don’t use Lighttable. But considering that it was one of the first text editors to target Clojure, I would assume there is a plugin available somewhere.

roelof14:11:59

I will look and hopefully find the parentheses problem

roelof14:11:04

thanks both

roelof14:11:36

stupid parentheses : now I have this problem: https://www.refheap.com/112023

roelof14:11:18

I think there is more wrong on the others line

roberto14:11:59

it looks like you are defining the arguments of your macro wrongly

roberto14:11:08

I would look into that

roberto14:11:36

I personally have never had the need to write a macro, so my macros fu is pretty terrible

roelof14:11:27

oke, sadly it's one of the chapters of the clojure koans

roelof14:11:09

and the challenge is to find the right arguments

roberto14:11:39

oh, I see your error

roberto14:11:48

you are calling your macro with more than one argument

roberto14:11:51

look inside your let

roelof14:11:22

oke, I know there is the error. When I change it to just rest form it compiles

roelof14:11:59

but then I see this error : java,lang.long cannot be cast to clojure.lang.IFn

roberto14:11:38

yep, that is a hint

roelof14:11:12

which I also do not understand

swizzard14:11:23

aw buddy, we’ve covered this simple_smile

roberto14:11:24

your macro is not expecting a function

roberto14:11:07

and rest is a function

roelof14:11:56

oke, maybe quote this then

swizzard14:11:58

but like in general, X cannot be cast to clojure.lang.IFn means X is the first thing in a list

swizzard14:11:00

and shouldn’t be

roberto14:11:25

step away for a while

roberto14:11:31

and don’t think about the mechanis

roberto14:11:37

think about what you are trying to do

roberto14:11:44

conceptually

roberto14:11:02

you are close, btw

roelof14:11:37

what I try to do it to "convert" (10 + (2 3) + (4 5)) to ( +( 10(+ ( 2 3) ( 4 5))))

roelof14:11:48

and then calculate it

roberto14:11:43

what are you trying to do with (apply r-infix rest form) ?

roberto14:11:59

if you didn’t have to use apply, how would you execute it?

roelof14:11:46

I try to make it work at the rest when (10 + (* 2 3) is already converted

roelof14:11:04

so the + ( 4 * 5) part

roelof14:11:15

must still be converted

roberto14:11:20

how would u execute it with apply?

roberto14:11:29

i mean without apply?

roberto14:11:42

for example: (apply + [ 1 2]) => (+ 1 2)

roelof14:11:15

a call to the function and then as argument the +( 4 * 5) part

roelof14:11:21

without apply ( first form ..... )

roberto14:11:48

fill in the blanks: (apply r-infix rest form) => (r-infix _)

roelof14:11:00

(first form first[1 2 ] second [1 2] )

roelof14:11:33

rest form ???

roelof14:11:46

r-infix (rest form) form

roelof14:11:19

@roberto: last one well or am i on the wrong track

roberto14:11:36

huh? that doesn’t look like clojure

roelof14:11:56

(r-infix (rest form) form))

roberto14:11:16

okay, now look at r-infix signature

roberto14:11:22

it only takes 1 arg

roberto14:11:25

and you are passing two

roelof15:11:09

oke, (r-infix (rest form))

roberto15:11:21

now, translate that to apply

roelof15:11:20

apply r-infix (rest form) ???

roberto15:11:47

now try it, and hopefully you get a different error this time

roelof15:11:47

I see now this : https://www.refheap.com/112024 so another error message

roberto15:11:16

yeah, this time it might be your logic, I’m not so sure why you are applying r-infix to others twice

roberto15:11:28

anyway, I need a coffee break

roelof15:11:11

I also. IM figthing the whole day against this one

roelof15:11:51

anyone else who can help me with this annoying problem

roelof15:11:54

hmmm, I did use instarepl of Light table and the problem is really which variable does have the value of the last part

roelof15:11:02

form seems to have the value of the "converted" form

roelof15:11:50

and the part + ( 4 * 5) seems to be gone

roelof15:11:43

wrong form looks like this : (10 + (2 3) + (4 5))

roelof15:11:31

anyone who can help me figure out what the right arguments of other is

nando22:11:25

I’m working through 4clojure and stuck on #30

nando22:11:29

Write a function which removes consecutive duplicates from a sequence.

nando22:11:12

The idea I have is to use map to iterate through the sequence and conj to a result vector only when % not= last result

nando22:11:31

But I can’t even get the function to return the vector without any when statement in the func

nando22:11:41

(defn mydedupe [s] (let [result []] (map #(conj result %) s) result))

nando22:11:06

Running (mydedupe [1 1 2 3 3 2 2 3]) returns an empty vector, when I expect it should return the same vector.

surreal.analysis22:11:42

result is an empty vector

surreal.analysis22:11:49

And also what you are returning

nando22:11:02

I’m just trying to get an interim function working here before adding in the when form

surreal.analysis22:11:40

Right, but your code is:

(let [result []]
  (do something with no side effects)
  result)

surreal.analysis22:11:54

You may want to look into into, if I'm reading what you're trying to do correctly

nando22:11:14

So what am I not understanding? Can I declare a local variable with let, modify it, and return the result?

surreal.analysis22:11:27

You cannot do that - collections are immutable

surreal.analysis22:11:51

(conj a-list "b") returns a new list

surreal.analysis22:11:06

a-list is still whatever it was before the function

nando22:11:20

I think I get it now

nando22:11:12

So how would I return the modified vector?

surreal.analysis22:11:42

Well, map takes a function and runs it against the the collection

surreal.analysis23:11:14

So while this doesn't really do what you want,

(defn mydedupe [s] (map identity s))
would be an example of you returning the same list

nando23:11:00

Ok, let me go back and try again. My brain is too used to mutable variables …

nando23:11:05

Let me ask a basic question. Is it a workable strategy in Clojure to set up a result variable in a function and then build up a result using a loop or recur construct (such as map) and some logic? I was trying to use a let in the above code block, but since they are immutable, that won’t work.