someone who can give me a hint. I tried (not ( and (not(every? true? args)) (not-any? true? args))
that one is good when doing all the test except [true, true, true]
which gives now true but it has to be false
@roelofw you're on the right track but you've made a mess of your logic so lets try to think of this problem in simpler terms
I know im on the right track because most of the tests are a success
I still have difficult to find out which function I need to use
I'm trying to figure out a good way of talking you through it without giving away the answer
oke
I making dinner right now so It can be that i do not respond quick
so what function would you use if the requirement was to return true if they were all true or all false?
every? Returns true if (pred x) is logical true for every x in coll, else false.
so you would write (or (every? true? args) (every? false? args))
?
but there is a function that already checks for equality
=
yes, I forget that all of then could be false
yep , 1 = 2
gives false
and 2 = 2
gives true
so far im with you
and you're getting a bunch of booleans so it's even easier because you don't really need a predicate function
oke, you you mean something like a map to compare the item with true or false ?
I was thinking about something like this (and (some true? args) (some? false args))
that sounds like you would need a predicate function which I just hinted you don't need
hmm, I have to do some comparison otherwise I do not know if something is true or false
so I cannot think of a solution without any predicate
im sorry
@gdeer81 I cannot think of a solution that you have in mind
can you give a hint how I can check this [ true, true, true]
is all true without a predicate
in my opinion = is also a predicate
or do you mean something like this (map (= true) args)
you don't need to map =
since it takes a variable number of arguments
okay I think I'm just making things confusing, lets go back to the thing you put earlier, (and (some true? args) (some false? args))
oke
which cases did that fail on?
moment, I have to try, this is new idea
it fails here :
FAIL in (test1) (1_half_truth_test.clj:7)
Half truth - test 1
expected: (= (half-truth true true true true) false)
actual: (not (= nil false))
did your function return nil?
yes, apperently , which is not so wierd.
Returns the first logical true value of (pred x) for any x in coll,
else nil. One common idiom is to use a set as pred, for example
this will return :fred if :fred is in the sequence, otherwise nil:
(some #{:fred} coll)
so some? i returning nill if a item is not found
what did you put?
it was probably because of your and statement, since one of your statements returns nil your and statement returned nil
the code we are talking about
(defn half-truth
"Solution to Problem 1:
Write a function which takes a variable number of booleans.
Your function should return true if some of the parameters
are true, but not all of the parameters are true.
Otherwise your function should return false."
[& args]
(and (some true? args) (some false? args)))
maybe a idea to check if the output is true or false ?
he, with this one all tests of the stanford are green :
(defn half-truth
"Solution to Problem 1:
Write a function which takes a variable number of booleans.
Your function should return true if some of the parameters
are true, but not all of the parameters are true.
Otherwise your function should return false."
[& args]
( true? (and (some true? args) (some false? args))))
chips, but now working here well :
(half-truth [false true false ])
=> false
if I read the desription that one schould be true
nope, the answer always is false
it seems that there cannot be a solution that works for all cases
@roelofw look at what you are passing as an argument in that call to half-truth
and what the defn is taking as an argument
There's nothing wrong with the code in the body of the defn
chips, the arguments are all seperate
yeah, its good to copy/paste the test cases from the problem so you don't make a typo and craft a solution for a totally different problem
this looks well to me :
(half-truth true true true )
=> false
(half-truth true true false )
=> true
(half-truth false true false )
=> true
(half-truth false false false )
=> false
so 2 out of the 4 challenges solved of the standford ones
@roelofw do you know why false false false
or true true true
was returning nil
until you added the true?
predicate around the and
?
Read the docs for some
and see if you can work out why
@agile_geek yes, see here of the docs of some? Returns the first logical true value of (pred x) for any x in coll,else nil
👍
so if there is nothing that is true when I do (some? true? [false, false, false])
then the answer will be nill
Yes and same if nothing false in the other branch of the and
@roelofw did you get a working solution yet? can I tell you the answer now?
And because and
stops once it's evaluated all the predicates (if they're all truthy) or on the first that is falsey and returns the value from the some
clause that's falsey you get nil
yep
@gdeer81 I have this as working solution :
(defn half-truth
"Solution to Problem 1:
Write a function which takes a variable number of booleans.
Your function should return true if some of the parameters
are true, but not all of the parameters are true.
Otherwise your function should return false."
[& args]
( true? (and (some true? args) (some false? args))))
okay, good now I won't feel bad if I accidentally give away the answer I had in mind
@gdeer81 is it a totally other answer then
I get this idea when you wrote : (or (every? true? args) (every? false? args))
hint 1: this problem can be solved with one function that is in core
yes, and somebody else said it could be done in 4 characters
but there are not much functions with 4 characters 🙂
well then that should narrow it down
write a function that returns all the functions in core that have exactly 4 characters
that is a difficult one
oke, with google I succeed
I can display al functions of core with (dir clojure.core)
there are 72
hmm, filter does not what I expect : (filter (fn[item] (= (count item) 4 )) (dir clojure.core))
just use this as a reference https://gist.github.com/gdeer81/3897e926a7dea79dd070607bcdb5a548
although this probably isn't helping you think about the problem in simpler terms
oke, and now I have to read a lot of pages
no, I see some candidates but no function who looks right
most promising looks not= but if I read the page I do not think its the one
some I tried and it is not the answer
what does the page for not= say?
Same as (not (= obj1 obj2))
hmm, maybe the args could be obj1 and something like [ true , true] as obj2 ?
or maybe something like this one (not= true true true true)
since it is referring to =
in the doc string you'd have to read the docs for = to understand how it is supposed to work
I look at the examples and these look very good :
(not= false false false )
=> false
(not= true false false )
=> true
(not= true true true )
=> false
(not= true false true )
=> true
so it will be (not= args)
a very short one
or you could just pass in the function itself without having to wrap it since #(not= %&)
is the same as just passing in that function itself
nope, this case is failing :
FAIL in (test2) (1_half_truth_test.clj:11)
Half truth - test 2
expected: (= (half-truth true false true) true)
actual: (not (= false true))
it gives false
and because there a two true and one false , t schould give true
replace args
with the value and you'll see why
or, what is args?
args is a vector so here [false false]
so when you do (not= args)
you are doing (not= [false false])
so I retract my statement above
chips , & args gives a vector and not the input itself 😞
#(not= %&)
is not the same as bare not=
take a look at apply
@roelofw
@gdeer81 this is also not working :
(defn half-truth
"Solution to Problem 1:
Write a function which takes a variable number of booleans.
Your function should return true if some of the parameters
are true, but not all of the parameters are true.
Otherwise your function should return false."
[& args]
#(not= %&))
it fails with this :
FAIL in (test1) (1_half_truth_test.clj:7)
Half truth - test 1
expected: (= (half-truth true true true true) false)
actual: (not (= #object[pset1.1_half_truth$half_truth$fn__205 0x1133976 "pset1.1_half_truth$half_truth$fn__205@1133976"] false))
@roelofw args
is being coerced into a sequence in you argument vector in the defn of half-truth
yes, That is what I said earlier, @agile_geek
@roelofw you've gone over your four character limit
yep
but you are calling half-truth
with a variable number of arguments and not=
takes a variable number of arguments so you don't need to coerce args to a sequence
you don't need to define a new function, just pass in not=
however, if you do want to wrap it in your own function you will have to unroll the args
@agile_geek so I can change [& args]
to [ args]
not if you want it in separate fn
as @gdeer81 said
just use not=
or use apply
to unroll args
so somehing like (not= (apply args))
????
if you keep your fn and change argument to [arg]
it will only expect one arg
No
@roelofw what does the doc string for apply say?
☝️
'(apply function args) `
so (apply (not= ) args)
close
it's the function you're applying not the evaluated function
remember putting a fn in parens makes Clojure evaluate it
@roelofw I'd watch lecture 1B here, you've made a similar mistake a couple times: https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-001-structure-and-interpretation-of-computer-programs-spring-2005/video-lectures/
the function is (not= args)
, Right ?
thats the evaluated function
i.e you are calling the fn not=
with the argument args
oke, so it would be (apply (not=) args)
that will give you an arity exception
yes, I does
That's still evaluating not=
but with no args
I will take a break, my head is spinning
So what happens if Clojure evaluates this (not=)
which gives an arity exception, and then tries to pass the result to (apply <result of calling not=> args)
apply wants a function and some arguments and I cannot see what the function is and what the arguments
What is the name of the function
not=
consider this example (+ [1 2 3])
how could you use apply to make + work with this vector?
there + is the function and [1 2 3 ] as a arguments I think
right
so (apply (+) [ 1 2 3 ] )
What is the reuslt of (+)
on its own
what does (+)
evaluate to?
but schould this not be in #beginner or #clojure ?
a function ??
(fn? (+))
vs (fn? +)
heading to lunch now, keep at it, you're almost there
I do not have that feeling
so if (+)
evaluates to 0
yes, I saw it on your code
then this (apply (+) [1 2 3])
is same as (apply 0 [1 2 3])
I think this will be (+ 1 2 3)
at the end
so something is still not right
the + has to stay
no, it will be (0 1 2 3)
apply only sees the substituted value
apply is not easy to understand
It is when you get the hang of it. At the moment you are passing it the result of evaluating a function, not the function itself
yeah but understanding apply is fundamental to lisp, understand it and other things will make sense too
i.e. in the + example you were evaluating + (+)
which is 0
and then passing that to apply
but 0
is not a function
hence the class cast exception in Clojurebot ☝️
what I want is something like this (fn [ acc item] ( acc + item)) Like I will do when using a map or reduce
@roelofw do you know other languages?
You could use reduce but apply is easier
a little bit ruby
@agile_geek that part I understand now
but I do not see how I can make it work with apply at this moment, sorry
apply is like the splat operator not_equal(*args)
So you need to pass apply
the unevaluated +
As you evaluate +
using parens (+)
, how would you pass +
not evaluated?
oke, so without the ()
Yep
so it would be `(apply + [1 2 3])
oke, so back to our code
So in your half-truth example you can use apply with not=
(apply not= args)
?
Try it
what does (apply not= [true true false])
give you
all test provided by the standford challenges are green
So now you know a way of calling a function that takes separate multiple arguments when you have a collection of arguments
yes, I learned a lot today from you and some others
now time for the dishes 😞
and tomorrow try to find out how to find the nth row of a pascal triangle
but I noticed how more you know of clojure how shorter the code it seems
All thanks and have a good day/evening
@roelofw I was going to ask you to now try and solve it with set theory
the same exercise with a set
?
yeah your function rolls up the arguments into a collection, so what happens if you called (set args)
?
( count (set(args) > 1)
out if my head
exactly right
so if there are more then 1 arg then there is one or more true or false
if there is only 1 the seq contains only true or false
okay, I updated my statement
@gdeer81 happy with the solution
the readability could be improved. how many items does the set have every time its suppose to return true?
more then1
but it will never have 3
nope,, its 1 or 2
1 if the items are all true or false, 2 if both
so what is more readable (= 2 (count (set args)))
or (> (count (set args)) 1)
?
I think the first
ofc things look a bit different with 0 or 1 args
well this is assuming that this is being called with (defn [& args] ...)
so the args are always being rolled up
I have asummed the same
So I have earned a C today 🙂
the point is that you've come away from this experience knowing more than you knew when you came in
yep, today I did
then today you get an A
learned to read stack traces and where the error can be found
learned apply
learned not=
I hope I can use it later on when needed
and you've learned when Clojure "rolls up" arguments and what that means
that will be the biggest challenge on the standford and 4clojure challenges
You've also learned the difference between a function and a function evaluation
that is why my head is spinning
😛
learned how to refractor functions so I can test them
I think it's confusing because as a beginner you're taught to always hug your functions
hug my functions ??
so (apply + [1 2 3])
looks weird because you want to hug that plus sign (+)
oke
this is what tripped me up when I started learning the ->
macro
oke, I use the ->> on my first attempt for a website but there I had not make this mistake
this was quite a thorough code review
yep
with a lot of explanation , I wonder if some of this schould not be in beginners or the clojure channel
Now with 4clojure I have to reimplement zipmap and with the stanford challenges I have to find the nth row of a pascal triangle
both not easy
but now time for family
@gdeer81 if you want and have time , do you want to review the last version of my paintings website : https://github.com/rwobben/paintings
yeah I'll take a look and see if I notice any glaring issues
thanks, do it only if you have time