99 lisp problems …. in F# Q11-20

Some more list functions!

 

Question 11

 

Modified run-length encoding.
Modify the result of problem P10 in such a way that if an element has no duplicates it is simply copied into the result list. Only elements with duplicates are transferred as (N E) lists.
Example:
* (encode-modified ‘(a a a a b c c a a d e e e e))
((4 A) B (2 C) (2 A) D (4 E))
1 2 3 4 5 6 7
type numEncodeItem<'T> =
| C of int * 'T
| V of 'T
let numenc2 (c,i) = if c <> 1 then C (c,i) else V i
let numEnc2 l = List.map numenc2 l
 
let res11 = res10 |> numEnc2
view raw gistfile1.txt This Gist brought to you by GitHub.

Continue reading

Posted in F# | Tagged , , | Leave a comment

99 lisp problems …. in F# Q6-10

 

Question 6

Find out whether a list is a palindrome.
A palindrome can be read forward or backward; e.g. (x a m a x).

 

1 2 3 4 5
let compareItems l m = List.fold2 (fun a l m -> if l = m then a else false) true l m
let isPalindrome a = a |> List.rev |> compareItems a
 
let res6T = isPalindrome [1;2;3;2;1]
let res6F = isPalindrome [1..5]
view raw gistfile1.fs This Gist brought to you by GitHub.

Checking that a list is a palindrome is fairly straight forward; First define a function compareItems that as the name suggests, goes through the items in a list and returns true if all items are equal.

Continue reading

Posted in F# | Tagged , , | Leave a comment

99 lisp problems …. in F#

First of all I’d like to welcome you the this blog. Hopefully I will be able to keep it up to date with the latest things I have been doing.

In the past few weeks I have been trying to familiarise myself more with F#. I had tried this already about a year ago, but it was in the middle of my studies and I couldn’t dedicate enough time on this task. Now I have the time so I’m giving it another go.  I picked up a copy of Real world functional programming and started going through it, but if you want to properly learn something you have to apply it. After some searching on the web I came across this list of 99 problems for lisp which is another functional language. So now I have the learning resource and some training exercises :)

 

Continue reading

Posted in F# | Tagged , , | Leave a comment

Hello world!

and we’re in business !!!!!

Posted in Uncategorized | 1 Comment