Query regarding an unusually behaving code

Bellow is my code for returning a list of digits given an integer.I'm unable to find any bug in it but somehow it crashes any computer I run it on when I try to calculate digs 100.Please tell me bug in my code. let digs 0 =[0] let digs x = (digs (x `div` 10)) ++ [(x `rem` 10)] digs 100 Thanks Abhishek

The problem is that you define digs twice and the second definition overshadows the first one. This makes your recursion infinite. You should define all cases at once like: let digs 0 = [0]; digs x = (digs (x `div` 10)) ++ [(x `rem` 10)] Greg On 13.11.2015 07:36, Abhishek Kumar wrote:
Bellow is my code for returning a list of digits given an integer.I'm unable to find any bug in it but somehow it crashes any computer I run it on when I try to calculate digs 100.Please tell me bug in my code.
let digs 0 =[0] let digs x = (digs (x `div` 10)) ++ [(x `rem` 10)] digs 100
Thanks Abhishek
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

This is because the second let redefines your first definition. Hence, you
don't have a base case anymore.
digs 0 =[]
digs x = (digs (x `div` 10)) ++ [(x `rem` 10)]
When I compile this with ghc though, it works fine.
This is how ghci works, I'd presume.
On Fri, Nov 13, 2015 at 12:06 PM, Abhishek Kumar
Bellow is my code for returning a list of digits given an integer.I'm unable to find any bug in it but somehow it crashes any computer I run it on when I try to calculate digs 100.Please tell me bug in my code.
let digs 0 =[0] let digs x = (digs (x `div` 10)) ++ [(x `rem` 10)] digs 100
Thanks Abhishek
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

In ghci you want to make a multi-line expression: :{ let digs 0 =[0] digs x = (digs (x `div` 10)) ++ [(x `rem` 10)] :} (Note we don't put "let" on the second line) tom
El 13 nov 2015, a las 01:47, akash g
escribió: let digs 0 =[0] let digs x = (digs (x `div` 10)) ++ [(x `rem` 10)]

GHCi doesn't quite support everything you could put in a source file. To do
what you want here, you need to use Haskell's alternative block syntax:
:{
let { digs 0 = [0]
; digs x = (digs (x `div` 10)) ++ [(x `rem` 10)]
}
:}
...yep, curly braces and semicolons just like C-family. :) It's intended
for machine-generated code, but works for this too. And in fact you
actually don't need multi-line, this is legal:
let { digs 0 = [0]; digs x = (digs (x `div` 10)) ++ [(x `rem` 10)] }
On Sat, Nov 14, 2015 at 9:35 PM,
In ghci you want to make a multi-line expression:
:{ let digs 0 =[0] digs x = (digs (x `div` 10)) ++ [(x `rem` 10)] :}
(Note we don't put "let" on the second line)
tom
El 13 nov 2015, a las 01:47, akash g
escribió: let digs 0 =[0] let digs x = (digs (x `div` 10)) ++ [(x `rem` 10)]
Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

Gah, got here late and missed that this was a response, not the problem
description. That's twice now I've put my foot in it. Stupid mailinglists,
with no edit buttons...
Well at least in both cases I've added SOMETHING useful. :)
On Sat, Nov 14, 2015 at 11:42 PM, Theodore Lief Gannon
GHCi doesn't quite support everything you could put in a source file. To do what you want here, you need to use Haskell's alternative block syntax:
:{ let { digs 0 = [0] ; digs x = (digs (x `div` 10)) ++ [(x `rem` 10)] } :}
...yep, curly braces and semicolons just like C-family. :) It's intended for machine-generated code, but works for this too. And in fact you actually don't need multi-line, this is legal:
let { digs 0 = [0]; digs x = (digs (x `div` 10)) ++ [(x `rem` 10)] }
On Sat, Nov 14, 2015 at 9:35 PM,
wrote: In ghci you want to make a multi-line expression:
:{ let digs 0 =[0] digs x = (digs (x `div` 10)) ++ [(x `rem` 10)] :}
(Note we don't put "let" on the second line)
tom
El 13 nov 2015, a las 01:47, akash g
escribió: let digs 0 =[0] let digs x = (digs (x `div` 10)) ++ [(x `rem` 10)]
Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
participants (5)
-
Abhishek Kumar
-
akash g
-
amindfv@gmail.com
-
Grzegorz Milka
-
Theodore Lief Gannon