Message Boards Message Boards

0
|
5594 Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:

simply right-reduce please

Posted 9 years ago

Hello,

this is a very basic and therefore somewhat embarrising one, but ... cannot figure it out.

I have a list, e.g, {2, 9, 2, 9, 1, 10, 1, 1, 3, 1, 8, 2, 8, 2} (representing a solution of a numbers puzzle but not important) and I need to make it the number 292911011318282. So I need to take an element multiply it by 10 if it is <10, otherwise 100 and add the rest of the list applying the same function. Therefore this seems to be the very basic right-reduce of functional programming and certainly there is that function somewhere in Mathematica but I do not see it and I fail misserably simulating it with a own function for quite some time now.

It should do (Scheme):

(define (right-reduce combine initial s) (if (empty-stream? s) initial (combine (head s) (right-reduce combine initial (tail s)))))

just for Lists in Mathematica (and of topic: are there (potentially) endless streams in Mathematica with lazy evaluation such as there are in Scheme or Haskell?).

I cannot solve my problem by hand as there are a few thousand solutions to be compared and I also should learn how to right-reduce in Mathematica so - please...

so embarrising.

POSTED BY: Erik Itter
2 Replies

I have a list, e.g, {2, 9, 2, 9, 1, 10, 1, 1, 3, 1, 8, 2, 8, 2} (representing a solution of a numbers puzzle but not important) and I need to make it the number 292911011318282

one way

lst = {2, 9, 2, 9, 1, 10, 1, 1, 3, 1, 8, 2, 8, 2};
FromDigits[lst]
(* 29292011318282 *)

another way

lst = {2, 9, 2, 9, 1, 10, 1, 1, 3, 1, 8, 2, 8, 2};
ToExpression@StringJoin[ToString[#] & /@ lst]
  (*292911011318282*)
POSTED BY: Nasser M. Abbasi
rightReduce[combine_, initial_, {head_, tail___}] := combine[head, rightReduce[combine, initial, {tail}]]
rightReduce[_, initial_, {}] := initial
POSTED BY: John Doty
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract