Fork me on GitHub
#beginners
<
2015-11-23
>
binduwavell05:11:02

If anyone is interested in working through the Clojure for the Brave and True exercises in a cloud IDE, I've created a setup for http://www.codenvy.com where you can do this. I've made a few videos to show how: Part 1: https://www.youtube.com/watch?v=avDgIn4c-oc Part 2: https://www.youtube.com/watch?v=iZBVt2l_vOs Part 3: https://www.youtube.com/watch?v=5V9S_P7qTGw

binduwavell05:11:21

Is late here so I'm off but feel free to PM if you are interested, have feedback, etc.

meikemertsch05:11:32

Great work @swizzard of explaining!

roelof07:11:28

I see something wierd. I have this function

roelof07:11:29

(let [not-a-symbol? (complement symbol?)] (map not-a-symbol? [:a 'b "c"]))

roelof07:11:53

according to repl the outcome is (true false true)

roelof07:11:12

but schould it now be the other way around

roelof07:11:39

I think I have to reread what a symbol is . I thought a symbol was :a

andrut07:11:14

:a is a keyword, not a symbol simple_smile

roelof07:11:58

oke, I use now google to find out what a symbol is

roelof07:11:51

anyone who knows a good site/tutorial what exactly a symbol is

agile_geek07:11:17

A symbol is usually something that resolves to a reference to either a function or a data value.

agile_geek07:11:43

i.e. it’s a ‘variable name'

seancorfield07:11:23

So b is a symbol - but you have to quote it to prevent evaluation hence 'b.

roelof07:11:06

oke, so everything that start with a character between a-z or A -Z is a symbol ?

agile_geek07:11:56

@roelof: plus *, +, !, -, _, ', and ?

agile_geek07:11:32

you can’t begin or end a symbol with :

agile_geek07:11:25

. and / are reserved (for Java interop and namespace qualification respectively)

agile_geek07:11:06

and \ is used to escape things like characters i.e. \c is the Character ‘c’ not the String “c"

agile_geek07:11:51

there are some reserved words of course, like ‘true’ and ‘false’ etc.

roelof07:11:00

thanks all

roelof08:11:20

another problem

roelof08:11:26

I have now this koan

roelof08:11:27

(defn is-even? [n] (if (= n 0) true (_ (is-even? (dec n)))))

roelof08:11:43

I have no clue what schould be the answer

roelof08:11:57

I know that (rem n 2) must be zero

roelof08:11:04

but why is there a dec

kauko08:11:37

Did you write that function yourself?

roelof08:11:25

nope, it's one of the clojure koans

roelof08:11:45

I have to figure out what schould be in the _ part

kauko08:11:52

I don’t really understand the full problem

roelof08:11:28

somehow I have to put something into the _ part so the function gives false on a odd number ?

andrut08:11:31

try to reverse the logic 😉

roelof08:11:11

@andrut : you mean give true on a even number ??

cjmurphy08:11:49

What's 'not' to regret about being told the answer before having spent some time 'nutting it out'?

roelof08:11:48

learn nothing but copy paste ?

cjmurphy08:11:29

As I remember I had trouble with this one too.

roelof08:11:03

I have problems to understand why you need to decrease a number to know if a number is even

roelof08:11:43

normally I would do ( rem number 2) if its zero its even , if it's 1 is not even

cjmurphy08:11:44

They are slowly getting you into a recursive mindset.

cjmurphy08:11:52

Recursion is weird yes - and the frustration is when you are getting better at it.

cjmurphy08:11:52

Koans are meant to be frustrating - the Buddhist ones as well as the Clojure ones.

roelof08:11:57

I thought I know recursion from my haskell days but I never seen this before

cjmurphy08:11:08

Well it is calling itself. I admit this one needs a 'light bulb' moment.

roelof08:11:52

oke, I will try to read up on recursion on clojure and hopefully I will have a "light bulb" moment

agile_geek08:11:09

I think solving this is not really a very good demonstration of recursion. It feels more like the author being ‘clever’ to me

cjmurphy08:11:19

Not a good practical demonstration, yet still I feel it helped me into the recursive mindset (such that I can be aware)

roelof08:11:29

thanks both. I will read up with recursion on clojure and will try to solve this then first on paper

agile_geek08:11:37

@roelof: not sure if this will help but think about what result should be returned if you input 1, then work out how you could just get this to occur by adding something at the placeholder. Then think about what the stack of function calls would be for 2

cjmurphy08:11:56

Better to think of the hints you got here - like the one about reversing the logic.

roelof08:11:13

oke,, as far as I know there are two ways of reversing true and false. That are not and complement

meikemertsch08:11:53

half true… not reverses the boolean, while complement gives you another function of which the evaluation will give you the opposite

cjmurphy08:11:41

@roelof - your thinking was perfect!

roelof09:11:48

oke so it not

roelof09:11:18

meikemertsch: I thought you were the whole day off till late in the afternoon

agile_geek09:11:28

@roelof: now think about why - see my hint about the function call stack.

meikemertsch09:11:30

just stopping by 😉

meikemertsch09:11:42

but the guys have it all covered simple_smile

meikemertsch09:11:02

just don’t let him be sloppy 😉 😈

roelof09:11:15

if I knew you could help me I would have asked you, sorry

meikemertsch09:11:32

but the guys are doing an awesome job here!!

meikemertsch09:11:13

No one is just giving you an answer, they’re asking great questions and make you think. It’s great to see

roelof09:11:22

if I use complement, I still have to evalute this ? as not give me direct the reverse of true or false

agile_geek09:11:38

BTW don’t try that with a -ve number!

roelof09:11:55

meikemertsch: that is what I like about clojure and this channel. No answers , just explanations

agile_geek09:11:09

@roelof: the Repl is your friend! 😄

agile_geek09:11:22

negative number

roelof09:11:57

correct, on a negative number the looping never ends

agile_geek09:11:11

as you are decrementing

agile_geek09:11:43

so you never reach the base condition (=n 0)

roelof09:11:23

wierd that some koans are very simple and some chapters are very hard

agile_geek09:11:55

That one is a little bit like the ‘there is no spoon’ moment in The Matrix!

roelof09:11:25

sorry I do not have seen that movie

roelof09:11:50

@meikemertsch: I hope you still are my "teacher"

cjmurphy09:11:39

The koans are the teacher, and the original Buddhist ones are to achieve the goal of life which is non attachment. Which is of course much harder than learning Clojure.

roelof09:11:22

yep, I never tried a Buddhist koan

meikemertsch09:11:43

@roelof. Sure, but the cool thing is you don’t have to stick only to me simple_smile you can tap into more than only one teacher simple_smile

roelof09:11:58

I take a break

meikemertsch09:11:59

This is not a marriage 😂

roelof09:11:10

no, im already married

cjmurphy09:11:18

roelof you have so many teachers. But I claim to be the original one who recommended doing koans to you!

meikemertsch09:11:23

Dropping out now again… see you later.. Guys, you’re doing a fine job, keep on doing it. It’s awesome 👍:skin-tone-2:

roelof09:11:24

see you later

roelof09:11:42

I will take a break so also dropping out

roelof14:11:30

why does this loop never ends : https://www.refheap.com/111993

sveri14:11:19

@roelof: you have two recursins in your function: 1. the recur and 2 the call to factorial

roelof14:11:02

oke, so I can delete the call to factorial

skadinyo14:11:48

@roelof have you see the example using loop in clojure cheat sheet?

roelof14:11:10

no, I will look at it

roelof14:11:24

@skadinyo: I have looked at it

roelof14:11:52

which example is the one which can tell me what I have done

skadinyo14:11:40

The last example by 'link' looks like your factorial.

samflores14:11:56

@roelof: you mixed two possible implementations of factorial: recursive and a loop (with loop recur)

roelof14:11:38

oke, I see it, that example makes a count but I can change it easily to multiply it

skadinyo14:11:22

Yes, in loop usually you need an accumulator.

skadinyo14:11:40

Its acc in the clojuredocs example

roelof14:11:29

thanks, I will try that way

roelof14:11:53

hmm, Light table needs a restart I think

roelof14:11:04

still no output

roelof14:11:56

oke, restarting LT gives output

roelof14:11:58

all thanks

roelof14:11:43

another problem

roelof14:11:04

but when I do (factorial 1000N)

roelof14:11:10

I see a integer overflow

roelof14:11:18

I do the clojure koans

skadinyo14:11:34

Check bigint in clojuredocs simple_smile

skadinyo14:11:56

Again, the last example.

samflores14:11:40

@roelof: you can just initialise you accumulator with 1N

skadinyo14:11:29

Whoops the last example does not seem to work. You could use samflores way, or you could add `'` to + or *. So instead of `*`, you can use `*'`

roelof14:11:27

@samflores: the N makes also a bigint of it

roelof14:11:16

thanks, learned another thing

samflores14:11:50

inside your loop iter and acc are bound to integers and factorials grow big very fast 😄

samflores14:11:32

you can also use 1M to create a BigDecimal, but it's not very useful for this particular case

roelof14:11:15

I know. I tried a time ago something like this and it could calculate till 45 if I right

roelof14:11:20

after that a error message

roelof15:11:24

How can I take care that in the named part there is no , between the named parts section

roelof15:11:30

I have now this ; "You can regain the full argument if you like arguing" (= {:original-parts ["Stephen" "Hawking"] :named-parts {:first "Stephen" :last "Hawking"}} (let [[first-name last-name :as full-name] ["Stephen" "Hawking"]] {:orginal-parts [first-name last-name] :named-parts {:first first-name :last last-name}} )

roelof15:11:47

Which gives : {:orginal-parts ["Stephen" "Hawking"], :named-parts {:first "Stephen", :last "Hawking"}}

roberto15:11:55

can you paste code inside a snippet: For example:

new line then code then new line then 

roberto15:11:10

oops : \

new line then code then new line then \

roelof15:11:15

I can place the code into refheap

roberto15:11:33

you can do this:

(defn my-fun []
  … 
)

roberto15:11:47

it is like mardown

roelof15:11:32

that gives now : ` {:orginal-parts ["Stephen" "Hawking"], :named-parts {:first "Stephen", :last "Hawking"}}

roelof15:11:23

instead of {:orginal-parts ["Stephen" "Hawking"], :named-parts {:first "Stephen" :last "Hawking"}}

roberto15:11:33

having , inside vectors or maps doesn’t really matter. [:a :b] == [:a, :b]

roelof15:11:32

oke, then there is another reason why the koan failed

roberto15:11:44

try running it inside a repl

roelof15:11:16

found it. A typo in the orginal-parts

roelof15:11:22

I forgot a i

roberto15:11:39

yeah, that happens

roelof15:11:26

sorry for the noise and thanks for the help

roberto15:11:01

you’re welcome

roelof15:11:47

time for a break and dinner

swizzard16:11:55

look legit to me

roelof16:11:55

thanks, I have the most doubt about the last one because there was only mention of test-adress

swizzard16:11:20

yeah, but that’s defined up top

roelof16:11:26

now something new for me refs simple_smile

samflores16:11:30

I believe you are supposed to change only the ___ regions of the koans

samflores16:11:56

this one expects you destructure the two structures given ["Test" "Testerson"] and test-address

roelof16:11:37

oke, so I have to use a let ?

samflores16:11:04

let is not the only place where you can use destructuring. the original koan is: (___ ["Test" "Testerson"] test-address). a good tip is to think what usually goes in the first element of a list 😉

swizzard17:11:20

(@roelof we were talking about this the other day—clojure makes a lot of assumptions about the first thing in a list)

roelof17:11:21

sorry, I do not understand which direction you want me to think

samflores17:11:14

in the same file look at the koan labeled "Whether in function definitions"

samflores17:11:12

I don't want to spoil your fun 😄

roelof17:11:48

I did solve that one this way : ((fn [[a b c]] (str "An Oxford comma list of " a ", " b ", and " c ".")) ["apples" "oranges" "pears"]))

roelof17:11:21

make a function with the same number of arguments as the orginal file

samflores17:11:10

actually that function takes only one argument, which is a vector with three items

samflores17:11:02

that's what destructuring does: it breaks a compound data structure anywhere you would bind it to an Var (a function args vector or a let bindings form)

samflores17:11:33

(anyone feel free to correct me if I'm not explaining it correctly)

roelof17:11:16

oke, then I need two functions, One for the ["Test", "Testerson"] part and one for the adress part

roelof17:11:23

First dinner

samflores17:11:11

no. you can destructure every argument of the function. you'll need a function that takes two arguments and destructure both

roelof17:11:03

something like ((fn [ [a b] [{street-address :street-address, city :city, state :state} test-address]] printing part ["Test" "Testerson"] test-address))

roelof17:11:25

can be that some ( are missing. I type it right here

roelof17:11:35

but BRB dinner

roelof17:11:37

@samflores: do I think now in the right direction

samflores17:11:26

yep. right direction, but there is a problem with your map destructuring

roelof17:11:24

oke, I think I can solve that by slowly made the destructuring and the print part

roelof17:11:14

@samflores: can you help me one more time

roelof17:11:29

When I do (fn[ [[a b]] ] (str a b)) ["Test" "Testerson"] I see the same array back and not a string

samflores17:11:03

there are two issues there:

roelof17:11:10

and when i do ((fn [ [[a b]] ] (str a )) ["Test" "Testerson"]) I see only a T

samflores17:11:42

well you figured out the first one: you were not calling the function

samflores17:11:12

the second you is that there are too many [] in your destructuring

roelof17:11:46

oke, I found out already , The first part of this challenge is ready