Win a copy of Machine Learning with R: Expert techniques for predictive modeling this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

4clojure exercise n.53

 
Greenhorn
Posts: 4
Scala Clojure Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, people!

I'm actually doing the 4clojure's problems and I'm at the 53 now. Was trying this kind of solution, what could be wrong with it?

It returns a nullpointer without a trace. Also, for some reason, the first line's (conj act prev) does not work - when I println it prints out this:


Thank you
P.S. Feel free to indicate me any stylistic/semantic errors.
 
Rancher
Posts: 352
21
Mac OS X Monad Clojure Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember that Clojure is an expression-based language using (mostly) pure functions and immutable data. That means that your function contains three expressions which are evaluated and the last expression is what the function returns.

So (conj act prev) is evaluated -- produces a new value without changing act or prev and then that new value is essentially discarded (because it is not the last expression in the function).

Then (println mas act col prev) is evaluated -- the side-effect of displaying the original values of the four arguments happens and it returns nil (which is discarded because it is not the last expression in the function).

Then your if expression is evaluated -- it is the last expression in the function so its value is returned to the caller.

Does that help?
 
lugaid vandroiy
Greenhorn
Posts: 4
Scala Clojure Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, a real lot! It's absurd to think that I could forget the most important characteristic of a language while programming in it...
I got it together, used let to define a couple of symbols to be used in the function's scope to reduce redundancy, and now it looks like this:

It does not do what I meant it to do, but I'm working on it.
It probably is not the best solution - I've seen one on github that is totally more readable and makes use of the ->> macro (which I just discovered), but I want to complete it anyways - if I do not become better at writing code in this language, then what's the point of 4clojure in the first place?
 
Sean Corfield
Rancher
Posts: 352
21
Mac OS X Monad Clojure Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Best way to learn something as novel as Clojure is by doing, so keeping on with the 4clojure exercises is great practice!

It is a fairly hard problem, especially to try to solve in an elegant, idiomatic way. According to 4clojure.com's logs, I solved this long enough ago that it wasn't tracking submissions and scores so I don't remember how I solved it. I just went back and solved it again and I'm not very pleased with my new solution but it didn't feel too horrible... until I looked at a couple of other people's solutions!

Keep us posted on your progress.
 
lugaid vandroiy
Greenhorn
Posts: 4
Scala Clojure Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fyeah, I did it!! Albeit my solution has not the elegance I'd like it to have, here is the code:

I removed an argument, so it's more succinct. Also, I believe:
> that the is a bit ugly, but I have no idea on how to attach an element at the end of the list.
> that the is a rough solution, but I do not know how to make a list with only one element otherwise.

It's a shame that my university is not teaching functional languages, forcing me to self-learn - but I like to do it anyway, so it's pretty OK, I guess.
Thank you for your help.
 
Sean Corfield
Rancher
Posts: 352
21
Mac OS X Monad Clojure Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use a vector [] instead of a list '(), conj will append to the end.
 
Would you like to try a free sample? Today we are featuring tiny ads:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!