Fork me on GitHub
#beginners
<
2016-11-13
>
roelofw17:11:09

Can someone help me figure out how to solve this one :

``````"They can easily emulate mapping"
(= '(0 1 4 9 16 25)
(map (fn [x] (* x x))
(range 6))
(for [x (range 6)]
__))  ``````

roelofw17:11:41

it is the second challenge from the seq-comprehension chapter of the clojure koans

roelofw17:11:25

I know that the map makes the output (0 1 4 9 16 25)

roelofw17:11:01

Does the for next loop makes also that output ?

mfikes18:11:52

@roelofw They are trying to show you that `for` implicitly acts like it is mapping a function in this case. Have you taken a look at what `for` does?

practicalli_john18:11:21

Wondering what the significance of a quote after a function means, for example in the source of the `range` function I saw this expression `(iterate inc' 0)`. I understand that ' before is a syntax shortcut for quote, is this the same if the quote is after the function name?

roelofw18:11:17

yep , I did .

roelofw18:11:46

I have seen examples where for does the same as a map function.

roelofw18:11:24

So I have to make a anymous function which does the same as in the map before it. @mfikes ?

mfikes18:11:01

@roelofw: no. While `map` applies a function, `for` is a macro that essentially evaluates its body once for each value, producing a sequence

mfikes19:11:13

@jr0cket: A lot of the numerical functions have "prime" versions that support arbitrary precision

roelofw19:11:12

@mfikes I mean the same as in this example with the let :

``````(for [x [0 1 2 3 4 5]
:let [y (* x 3)]
:when (even? y)]
``````

mfikes19:11:59

@roelofw you could use a `:let` in the `for` to bind the square of `x` to a symbol, but there is a simpler way...

mfikes19:11:07

Do you have a solution using `:let`?

roelofw19:11:33

I don't have a solutioin yet, I try to find out what is expected here

mfikes19:11:46

What would you do with this, for example @roelofw

``````(for [x (range 6)
:let [y (* x x)]]
,,,)
``````

mfikes19:11:06

What would you put in the `,,,`

roelofw19:11:52

I think it depends on what the expected output is , @mfikes

practicalli_john19:11:59

@mfikes ah, so there is inc and inc', that makes sense. Thanks

mfikes19:11:46

@roelofw: assuming you want `(0 1 4 9 16 25)`

roelofw19:11:54

I would use the identity function

mfikes19:11:27

@roelofw: that would produce a sequence of 6 values, where each value is the identity function. Whatever you put in the body of the `for` is what will be evaluated (not called) and included in the output sequence

mfikes19:11:32

@roelofw: in the example above with `:let`, for the first element in the output sequence (associated with `x` being bound to `0`), the symbol `y` will be bound to the value `0`. Then `y` will be bound to `1`, and then `4`, ...

roelofw19:11:24

yep, that far I understand

roelofw19:11:47

But I m confused to what must be with the ,,, now

mfikes19:11:02

Perhaps a simpler form to consider:

``````(for [x (range 6)]
x)
``````

mfikes19:11:00

Then you can vary what is output

``````(for [x (range 6)]
(- x))
``````

roelofw19:11:41

The first one gives a output of ( 0 1 2 3 4 5 ) and the second one ( 0 -1 -2 -3 -4 -5 -6 ) @mfikes

roelofw19:11:49

mfikes19:11:14

Right. Instead of `x`, or the negative of `x`, you need the square of `x`.

roelofw19:11:58

Then I think I can do

``(for [x (range 6)]  x * x )  ``

mfikes19:11:33

Almost. Just need to use Lisp prefix notation for the multiplication.

roelofw19:11:10

Oke, so ` (for [x (range 6) ( * x x )) `

roelofw19:11:57

which is the same as `(for [x (range 6) (fn [x] (* x x )))` if I do not forget a ( @mfikes

roelofw19:11:22

but then the ,, has to be replaced with nothing as I understand what you mean @mfikes

mfikes19:11:33

@roelof No… the body of the `for` loop needs to contain the expression that will be used to form the elements of the output sequence. You need to move your square expression there.

roelofw19:11:14

oke, and I can remove the let expression.

roelofw19:11:28

sometimes clojure can be very confusing

mfikes19:11:36

@roelof This is very close to correct:

``````(for [x (range 6)]  x * x )
``````
but instead, you write `x * x` as `(* x x)`

roelofw19:11:13

I did that on my last attempt

mfikes19:11:47

@roelof `for` is actually a fairly complicated macro. The mechanics of the `map` function, on the other hand, is easier to understand IMHO.

roelofw19:11:26

yes, I find this chapter of the clojure koans very difficult

roelofw19:11:42

The rest was fairly easy