This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-12-12
Channels
- # admin-announcements (1)
- # bangalore-clj (13)
- # beginners (149)
- # boot (123)
- # cider (7)
- # clojure (167)
- # clojure-brasil (3)
- # clojure-greece (1)
- # clojure-korea (2)
- # clojure-new-zealand (2)
- # clojure-russia (70)
- # clojure-sanfrancisco (3)
- # clojure-spec (84)
- # clojure-uk (36)
- # clojurescript (300)
- # code-reviews (242)
- # community-development (34)
- # core-async (4)
- # css (1)
- # cursive (37)
- # datascript (1)
- # datomic (20)
- # defnpodcast (1)
- # dirac (15)
- # events (7)
- # garden (12)
- # hoplon (100)
- # lein-figwheel (11)
- # off-topic (2)
- # om (69)
- # om-next (3)
- # onyx (86)
- # planck (14)
- # proton (4)
- # protorepl (1)
- # quil (2)
- # re-frame (53)
- # rum (3)
- # untangled (1)
- # vim (50)
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'm trying to figure out a good way of talking you through it without giving away the answer
so what function would you use if the requirement was to return true if they were all true or all false?
and you're getting a bunch of booleans so it's even easier because you don't really need a predicate function
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
can you give a hint how I can check this [ true, true, true]
is all true without a predicate
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))
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))
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)
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)))
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))))
@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
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
@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
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
@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))))
write a function that returns all the functions in core that have exactly 4 characters
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
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
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))
@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
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
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
if you keep your fn and change argument to [arg]
it will only expect one arg
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/
thats the evaluated function
i.e you are calling the fn not=
with the argument args
That's still evaluating not=
but with no args
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
right
What is the reuslt of (+)
on its own
so if (+)
evaluates to 0
then this (apply (+) [1 2 3])
is same as (apply 0 [1 2 3])
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
You could use reduce but apply is easier
@agile_geek that part I understand now
So you need to pass apply
the unevaluated +
As you evaluate +
using parens (+)
, how would you pass +
not evaluated?
So in your half-truth example you can use apply with not=
Try it
what does (apply not= [true true false])
give you
So now you know a way of calling a function that takes separate multiple arguments when you have a collection of arguments
yeah your function rolls up the arguments into a collection, so what happens if you called (set args)
?
the readability could be improved. how many items does the set have every time its suppose to return true?
well this is assuming that this is being called with (defn [& args] ...)
so the args are always being rolled up
the point is that you've come away from this experience knowing more than you knew when you came in
I think it's confusing because as a beginner you're taught to always hug your functions
oke, I use the ->> on my first attempt for a website but there I had not make this mistake
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
@gdeer81 if you want and have time , do you want to review the last version of my paintings website : https://github.com/rwobben/paintings